只用一个类编写的Java猜谜游戏
导入Java . util . hashmap;
导入Java . util . random;
导入Java . io . buffered reader;
导入Java . io . inputstreamreader;
公共类石头{
公共静态void main(String []agrs)引发异常{
map ST = new HashMap();
圣普特(1,“石头”);
St.put(2,“剪刀”);
圣普特(3,“布”);
Random co = new Random();
while(true){
System.out.println("请输入你的选择1:石头2:剪刀3:布4:退出");
int x = math . ABS(co . nextint()% 3)+1;
//system . out . println(x);//随机选择测试计算机
buffered reader me = new buffered reader(new InputStreamReader(system . in));
string an = me . readline();
尝试{
if(an . length()& gt;1 | | integer . parse int(an)& lt;1||Integer.parseInt(an)>4){
System.out.println("请输入正确选择");
继续;
}
}catch(异常e){
System.out.println("请输入正确选择");
继续;
}
int z = integer . parse int(an);
if(z==4){
返回;
}
否则{
if(x==z){
system . out . println(" computer:"+ST . get(x));
system . out . println(" Yours:"+ST . get(z));
system . out . println(" sum ");
}
else if(x = = 1 & amp;& ampz==2){
system . out . println(" computer:"+ST . get(x));
system . out . println(" Yours:"+ST . get(z));
System.out.println("你赢了");
}
else if(x = = 1 & amp;& ampz==3){
system . out . println(" computer:"+ST . get(x));
system . out . println(" Yours:"+ST . get(z));
System.out.println("你输了");
}
else if(x = = 2 & amp;& ampz==1){
system . out . println(" computer:"+ST . get(x));
system . out . println(" Yours:"+ST . get(z));
System.out.println("你赢了");
}
else if(x = = 2 & amp;& ampz==3){
system . out . println(" computer:"+ST . get(x));
system . out . println(" Yours:"+ST . get(z));
System.out.println("你输了");
}
else if(x = = 3 & amp;& ampz==1){
system . out . println(" computer:"+ST . get(x));
system . out . println(" Yours:"+ST . get(z));
System.out.println("你赢了");
}
否则{
system . out . println(" computer:"+ST . get(x));
system . out . println(" Yours:"+ST . get(z));
System.out.println("你输了");
}
}
system . out . println();
system . out . println();
}
}
}