Discuss / Java / 换种思路

换种思路

Topic source

闪闪凌

#1 Created at ... [Delete] [Delete and Lock User]
System.out.println("玩个游戏吧!!");String[] selectedList = new String[4];selectedList[1] = "剪子";selectedList[2] = "石头";selectedList[3] = "布";for (int i = 1; i < selectedList.length; i++) {    System.out.println(i + ":" + selectedList[i]);}Scanner scanner = new Scanner(System.in);System.out.print("输入你的选项:");int yourChoice = scanner.nextInt();System.out.println("你的选项" + yourChoice + ":" + selectedList[yourChoice]);int sysChoice = 1 + (int) (Math.random() * (3 - 1));System.out.println("系统选项" + sysChoice + ":" + selectedList[sysChoice]);//已知规律结果2,3,4,5,6,排除不需要刻意处理的String result = switch (yourChoice + sysChoice) {    case 3, 5 -> yourChoice > sysChoice ? "您赢了" : "您输了";    case 4 -> yourChoice == sysChoice ? "平局" : (yourChoice < sysChoice ? "您赢了" : "您输了");    default -> "平局";};System.out.println("游戏结果:" + result);System.out.println("游戏结束");

  • 1

Reply