1 import java.util.Scanner; 2 3 import java.util.Random; 4 5 public class TestGuess{ 6 7 public static void main(String[] args){ 8 9 Scanner yc = new Scanner(System.in); 10 Random cy = new Random(); 11 12 System.out.println("=============================================="); 13 System.out.println("====================人机猜拳=================="); 14 System.out.println("=============================================="); 15 System.out.println("====数字1 = 剪刀 数字2 = 石头 数字3 = 布===="); 16 System.out.println("=============================================="); 17 18 int computerCount = 0; //电脑的获胜次数 19 int playerCount = 0; //玩家的获胜次数 20 21 for(int i = 1;i <= 3;){ 22 23 int computer = cy.nextInt(3) + 1; //表示随机获得0、1、2;追加1后,变为1、2、3 24 25 System.out.println("请玩家输入对应的数字:"); 26 int player = yc.nextInt(); 27 28 //比较猜拳 29 if(computer == player){ //平局 30 31 System.out.println("平局,再接再厉!"); 32 continue; //不加 33 34 }else if((player == 1 && computer == 3)||(player == 2 && computer == 1)||(player == 3 && computer == 2)){ 35 36 System.out.println("恭喜,您赢得了对局!"); 37 playerCount++; //玩家胜率+1 38 39 }else{ 40 41 System.out.println("很遗憾,您未赢得对局!"); 42 } 43 44 //判断双方是否已经存在其中一方连胜两局的情况 45 if(playerCount == 2||computerCount == 2){ 46 break; 47 } 48 49 i++; 50 } 51 52 if(playerCount == 2){ 53 54 System.out.println("游戏结束,玩家获胜!"); 55 56 }else{ 57 58 System.out.println("游戏结束,电脑获胜!"); 59 } 60 61 } 62 } *如有不足 还请多多指点!
原文地址:https://www.cnblogs.com/Her4c/p/12283921.html
时间: 2024-11-23 21:12:20