import java.io.*;
import java.util.*;
class cai
{
String s;
int []suijishu;
int []shurushu;
int s1;
public static void main(String[] args) //main
{
int i;
int j;
int k;
cai cai=new cai();
cai.suijishu=new int[4];
cai.shurushu=new int[4];
HashSet hs=new HashSet();//利用HashSet随机产生四个不同的数
do
{
hs.add(cai.suiji());
}
while (hs.size()<=3);
Iterator it = hs.iterator();
for (i=0;i<4 ;i++ )
{
cai.suijishu[i]=(Integer)it.next();//将HashSet里面的数强制转换为整型数
}
//System.out.println("HashSet随机产生的数是:"+hs);
//System.out.println("HashSet随机数的长度是:"+hs.size());
System.out.println("1.当游戏开始后,系统会产生一个四位随机整数");
System.out.println("2.输入你要猜的四位数");
System.out.println("3.系统返回你所得的结果,并给出相应的评语");
System.out.println("4.nA表示位置和数都对的个数,nB代表数字对但位置不对的个数");
System.out.println();
for (j=1; j<6; j++)
{
System.out.println("输入四位数:");
for (i=0;i<4 ; i++)
{
cai.shurushu[i]=Integer.parseInt(cai.shuru());
}
System.out.println("结果为:");
cai.bijiao(cai.suijishu,cai.shurushu);
if (cai.s1==4)
{
break;
}
}
System.out.println("");
}
public int suiji()
{
Random r=new Random();
int sjs=r.nextInt(9);
return sjs;
}
public String shuru()
{
try
{
InputStreamReader inputs=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(inputs);
s=input.readLine();
}catch(IOException e)
{System.out.println("请输入整数!");}
return s;
}
public void bijiao(int []suijishu,int []shurushu)//比较
{
int s2=0;
int i;
int j;
for (i=0; i<4;i++ )
{
for (j=0;j<4 ;j++ )
{
if (suijishu[i]==shurushu[j]&&i==j)
{
s1=s1+1;
}else if (suijishu[i]==shurushu[j])
{
s2=s2+1;
}
}
}
System.out.println(s1+"A"+s2+"B");
if (s1==0)
{
System.out.println("你太笨了!好好动脑筋呀!");
}else if(s1==1)
{System.out.println("不太好,继续努力!");
}else if (s1==2)
{
System.out.println("还凑和,继续努力!");
}else if (s1==3)
{
System.out.println("快猜对了,加油啊!");
}else if (s1==4)
{
System.out.println("恭喜你,猜对了!");
}
}
}
import java.util.*;
class cai
{
String s;
int []suijishu;
int []shurushu;
int s1;
public static void main(String[] args) //main
{
int i;
int j;
int k;
cai cai=new cai();
cai.suijishu=new int[4];
cai.shurushu=new int[4];
HashSet hs=new HashSet();//利用HashSet随机产生四个不同的数
do
{
hs.add(cai.suiji());
}
while (hs.size()<=3);
Iterator it = hs.iterator();
for (i=0;i<4 ;i++ )
{
cai.suijishu[i]=(Integer)it.next();//将HashSet里面的数强制转换为整型数
}
//System.out.println("HashSet随机产生的数是:"+hs);
//System.out.println("HashSet随机数的长度是:"+hs.size());
System.out.println("1.当游戏开始后,系统会产生一个四位随机整数");
System.out.println("2.输入你要猜的四位数");
System.out.println("3.系统返回你所得的结果,并给出相应的评语");
System.out.println("4.nA表示位置和数都对的个数,nB代表数字对但位置不对的个数");
System.out.println();
for (j=1; j<6; j++)
{
System.out.println("输入四位数:");
for (i=0;i<4 ; i++)
{
cai.shurushu[i]=Integer.parseInt(cai.shuru());
}
System.out.println("结果为:");
cai.bijiao(cai.suijishu,cai.shurushu);
if (cai.s1==4)
{
break;
}
}
System.out.println("");
}
public int suiji()
{
Random r=new Random();
int sjs=r.nextInt(9);
return sjs;
}
public String shuru()
{
try
{
InputStreamReader inputs=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(inputs);
s=input.readLine();
}catch(IOException e)
{System.out.println("请输入整数!");}
return s;
}
public void bijiao(int []suijishu,int []shurushu)//比较
{
int s2=0;
int i;
int j;
for (i=0; i<4;i++ )
{
for (j=0;j<4 ;j++ )
{
if (suijishu[i]==shurushu[j]&&i==j)
{
s1=s1+1;
}else if (suijishu[i]==shurushu[j])
{
s2=s2+1;
}
}
}
System.out.println(s1+"A"+s2+"B");
if (s1==0)
{
System.out.println("你太笨了!好好动脑筋呀!");
}else if(s1==1)
{System.out.println("不太好,继续努力!");
}else if (s1==2)
{
System.out.println("还凑和,继续努力!");
}else if (s1==3)
{
System.out.println("快猜对了,加油啊!");
}else if (s1==4)
{
System.out.println("恭喜你,猜对了!");
}
}
}