package javaapplication4;
import javax.swing.JOptionPane;
public class GuessNumber {
public static String check(String answer,String choice)
{
int i,count1=0,count2=0;
int j;
String real;
for(i=0;i { if(choice.charAt(i)==answer.charAt (i)) count1++; } for(i=0;i for(j=0;j { if(choice.charAt(i)==answer.charAt(j)&&choice.charAt(i)!=answer.charAt (i)) count2++; } real=count1+"A"+count2+"B"; return real; } public static void main(String[] args) { String str ="",answer =""; int i,j,k,length,array[] = {0,1,2,3,4,5,6,7,8,9}; length=array.length; for(i=0;i<4;i++) { j=(int)(length*Math.random()); answer=answer+array[j]; length--; array[j]=array[length]; } JOptionPane.showMessageDialog(null," 游戏开始!"); k=0; while(!str.equals("4A0B")) { String choice = JOptionPane.showInputDialog("请输入四个不同的数字!"); str=GuessNumber.check(answer,choice); JOptionPane.showMessageDialog(null," 这次您的结果为:"+str); k++; } JOptionPane.showMessageDialog(null," 恭喜您答对了,一共猜了"+k+"次"); } }