Java 实现简单的人机猜拳游戏

 import java.util.Scanner;

 import java.util.Random;

 public class TestGuess{

     public static void main(String[] args){

         Scanner yc = new Scanner(System.in);
Random cy = new Random(); System.out.println("==============================================");
System.out.println("====================人机猜拳==================");
System.out.println("==============================================");
System.out.println("====数字1 = 剪刀 数字2 = 石头 数字3 = 布====");
System.out.println("=============================================="); int computerCount = 0; //电脑的获胜次数
int playerCount = 0;  //玩家的获胜次数 for(int i = 1;i <= 3;){ int computer = cy.nextInt(3) + 1; //表示随机获得0、1、2;追加1后,变为1、2、3 System.out.println("请玩家输入对应的数字:");
int player = yc.nextInt(); //比较猜拳
if(computer == player){ //平局 System.out.println("平局,再接再厉!");
continue; //不加 }else if((player == 1 && computer == 3)||(player == 2 && computer == 1)||(player == 3 && computer == 2)){ System.out.println("恭喜,您赢得了对局!");
playerCount++; //玩家胜率+1 }else{ System.out.println("很遗憾,您未赢得对局!");
} //判断双方是否已经存在其中一方连胜两局的情况
if(playerCount == 2||computerCount == 2){
break;
} i++;
} if(playerCount == 2){ System.out.println("游戏结束,玩家获胜!"); }else{ System.out.println("游戏结束,电脑获胜!");
} }
} *如有不足 还请多多指点!
上一篇:编写高质量代码:改善Java程序的151个建议(第5章:数组和集合___建议75~78)


下一篇:linux安装python