1 package day03.d3.xunhuankongzhi; 2 3 import java.util.Scanner; 4 5 public class CaiQuan { 6 7 public static void main(String[] args) { 8 Scanner sc = new Scanner(System.in); 9 String[] arr = { "石头", "剪刀", "布" };10 while (true) {11 System.err.println("请选择你要出的手势:");12 int you;13 for(;;){14 you = sc.nextInt();15 if(you>=0&&you<3){16 System.err.println("你出的是" + arr[you]);17 break;18 }else{19 System.err.println("你出的手势无效,请重新选择你要出的手势!");20 continue;21 } 22 }23 24 int comp = (int) (Math.random() * 3);25 System.err.println("电脑出的是:" + arr[comp]);26 padDuan(you, comp);27 System.err.println("是否继续下一局(Y/N):");28 String str;29 boolean bool;30 for (;;) {31 str = sc.nextLine();32 if (str.equals("Y")) {33 bool = true;34 break;35 } else if (str.equals("N")) {36 bool = false;37 break;38 } else {39 System.err.println("输入有误,请重新输入!是否继续下一局(Y/N):");40 continue;41 }42 }43 44 if (bool) {45 46 } else {47 break;48 }49 50 }51 }52 53 public static void padDuan(int x, int y) {54 if (x == y) {55 System.err.println("英雄所见略同!");56 } else if ((x == 0 && y == 1) || (x == 1 && y == 2)57 || (x == 2 && y == 0)) {58 System.err.println("恭喜你,你赢了!");59 } else {60 System.err.println("很遗憾,你输了,请再接再厉!");61 }62 }63 }