public class caiquan{
public static void main(String[] args){
System.out.println("人机大战");
Scanner scanner=new Scanner(System.in);
String player="";
String computer="王者";
System.out.println("请输入玩家的名字");
player=scanner.next();
System.out.println(player+"VS"+computer);
System.out.println("游戏开始?y/n");
String answer=scanner.next();
if("y".equalsIgnoreCase(answer)){
System.out.println("游戏开始");
System.out.println("游戏规则:");
System.out.println("1.石头 2.剪刀 3.布");
System.out.println("请玩家出拳:");
int playfist=scanner.nextInt();
switch(playfist){
case 1:System.out.println("玩家出石头");break;
case 2:System.out.println("玩家出剪刀");break;
case 3:System.out.println("玩家出布");break;
default:System.out.println("游戏错误");
}
int computerfist=(int)(Math.random()*3+1);
switch(computerfist){
case 1:System.out.println("电脑出石头");break;
case 2:System.out.println("电脑出剪刀");break;
case 3:System.out.println("电脑出布");break;
}
if(playfist==1){
if(computerfist==1){
System.out.println("平局");
}else if(computerfist==2){
System.out.println(player+"赢");
}else if(computerfist==3){
System.out.println("王者赢");}
else if(playfist==2){
if(computerfist==1){
System.out.println("王者赢");
}else if(computerfist==2){
System.out.println("平局");
}else if(computerfist==3){
System.out.println(player+"赢");}
else if(playfist==3){
if(computerfist==1){
System.out.println(player+"赢");
}else if(computerfist==2){
System.out.println("王者赢");
}else if(computerfist==3){
System.out.println("平局");}
else{
System.out.println("由于出拳规则,游戏结束");
}
}
}
else{
System.out.println("游戏结束");}
}
}
if语句的猜拳游戏
最新推荐文章于 2023-06-15 10:02:31 发布