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<choice.length();i++)
{
if(choice.charAt(i)==answer.charAt (i))
count1++;
}
for(i=0;i<choice.length();i++)
for(j=0;j<answer.length();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);
简单的java游戏代码 k++;
}
JOptionPane.showMessageDialog(null," 恭喜您答对了,一共猜了"+k+"次");
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论