close
import java.util.*;
public class test6
{
public static void main(String args[])
{
int n, m, a = 0, b = 0, t;
String input, password;
char[] pwTemp = new char[4];
char[] temp = new char[4];
Random random = new Random();
for(;;)
{
int number = random.nextInt(9999);
password = Integer.toString(number);
for(;password.length() != 4;)
password = "0"+password;
for(m = 0; m <= 3; m++)
pwTemp[m] = password.charAt(m);
if(pwTemp[0]==pwTemp[1] || pwTemp[0]==pwTemp[2] || pwTemp[0]==pwTemp[3] || pwTemp[1]==pwTemp[2] || pwTemp[1]==pwTemp[3] || pwTemp[2]==pwTemp[3])
continue;
else
break;
}
System.out.println("猜數字吧");
System.out.println("注意:數字長必須為四,且數字不可重複");
Scanner x = new Scanner(System.in);
for(t = 1;;t++)
{
input = x.next();
if(input.length() != 4)
{
System.out.println("格式錯誤,請重新輸入");
t--;
continue;
}
for(n = 0; n <= 3; n++)
{
temp[n] = input.charAt(n);
for(m = 0;m <= 3; m++)
{
if(temp[n] == pwTemp[m] && m==n)
a++;
if(temp[n] == pwTemp[m] && m!=n)
b++;
}
}
if(temp[0]==temp[1] || temp[0]==temp[2] || temp[0]==temp[3] || temp[1]==temp[2] || temp[1]==temp[3] || temp[2]==temp[3])
{
System.out.println("格式錯誤,請重新輸入");
b = a = 0;
t--;
continue;
}
System.out.println(a+"A"+b+"B");
if(a == 4)
break;
else
b = a = 0;
}
System.out.println("恭喜你猜對了\n你共猜了"+t+"次");
}
全站熱搜