You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中如何重启Switch所在循环?石头剪刀布游戏问题求助

解决石头剪刀布游戏无效输入重启当前回合问题

你的代码核心问题是:当用户输入无效选项时,程序没有跳过后续的回合逻辑(比如PC选择、计分),还会让回合数i递增,相当于直接消耗了一轮,没有实现"重启当前循环"的效果。

关键修改点:

  • 将用户输入的逻辑改成循环,直到输入1-3的有效选项为止
  • 输入无效时,不执行后续的PC选择判断和计分,直接重新获取输入
  • 避免无效输入消耗回合数

修改后的完整代码:

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissors {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Hey player enter your name");
        String name = sc.nextLine();
        name = name.toLowerCase();
        int human_score = 0;
        int pc_score = 0;

        System.out.println("How many rounds u want to play " + name);
        byte rounds = sc.nextByte();
        if (rounds > 10) {
            System.out.println("Please choose up to 10 rounds. Above is not allowed");
            System.out.println("Exiting the game");
            System.exit(0);
        }

        int i = 1;
        while (i <= rounds) {
            Random rand = new Random();
            int pc = rand.nextInt(3) + 1;
            System.out.println("What u choose? (1=石头, 2=布, 3=剪刀)");

            int human = -1;
            // 循环获取输入直到有效
            while (true) {
                if (sc.hasNextInt()) {
                    human = sc.nextInt();
                    if (human >= 1 && human <= 3) {
                        break; // 输入有效,退出输入循环
                    } else {
                        System.out.println("Invalid Choice,请输入1-3之间的数字");
                    }
                } else {
                    // 处理非整数输入,清空无效输入
                    System.out.println("Invalid Choice,请输入数字");
                    sc.next();
                }
            }

            // 输出用户选择
            switch (human) {
                case 1 -> System.out.println("You choose rock");
                case 2 -> System.out.println("You choose paper");
                case 3 -> System.out.println("You choose scissors");
            }

            // 输出PC选择
            switch (pc) {
                case 1 -> System.out.println("I choose rock");
                case 2 -> System.out.println("I choose paper");
                case 3 -> System.out.println("I choose scissors");
            }

            // 判断胜负并计分
            if (human == pc) {
                System.out.println("Its a draw");
            } else if (human == 1 && pc == 2) {
                System.out.println("You lost. Better luck next time");
                pc_score += 1;
            } else if (human == 1) {
                System.out.println("Congrats. You won.");
                human_score += 1;
            } else if (human == 2 && pc == 1) {
                System.out.println("Congrats. You won.");
                human_score += 1;
            } else if (human == 2) {
                System.out.println("You lost. Better luck next time");
                pc_score += 1;
            } else if (human == 3 && pc == 1) {
                System.out.println("You lost. Better luck next time");
                pc_score += 1;
            } else if (human == 3) {
                System.out.println("Congrats. You won.");
                human_score += 1;
            }

            i += 1;
        }

        // 输出最终结果
        if (human_score == pc_score) {
            System.out.println("Its a draw. Wanna play again?");
        } else if (human_score > pc_score) {
            System.out.println("Congrats you won. Wanna challenge again?");
        } else {
            System.out.println("Skill issue. This code is fine");
        }

        System.out.println("PC\t" + name + "\n" + pc_score + "\t" + human_score);
        sc.close();
    }
}

额外说明:

  • 新增了非整数输入的处理逻辑,避免用户输入字母等内容导致程序崩溃
  • 输入提示明确了1-3对应的选项,减少用户误操作
  • 只有当输入有效时,才会执行后续的回合逻辑,不会消耗当前回合数

内容的提问来源于stack exchange,提问作者Tomu is here

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 17:02:10