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

如何在Eclipse中为switch语句添加循环实现猜数字游戏

搞定你的猜数字游戏循环问题

嘿,我明白你现在的困惑啦——写了switch逻辑但不知道怎么让程序重复运行直到猜对对吧?咱们来一步步把循环加上,顺便修正个小细节:你现在case2里的提示说对了还让玩家再试,这可不太对哦😉

核心思路:用循环包裹输入和判断逻辑

咱们可以用do-while循环,因为玩家至少得猜一次,这种循环刚好满足“先执行一次代码,再判断要不要继续”的需求。具体步骤是:

  • 定义一个布尔变量标记是否猜对,比如boolean isGuessed = false
  • 把输入数字和switch判断的代码放进do块里
  • 在猜对的case(也就是你的case2)里,把isGuessed设为true,这样循环就会终止
  • 循环的条件是!isGuessed(没猜对就继续)

修改后的完整代码

import java.util.Scanner;

public class GuessNumberGame {
    public static void main(String[] args) {
        System.out.println("Hello traveler.. Please enter your.. your... name");
        Scanner in = new Scanner(System.in);
        String userName = in.nextLine();
        System.out.println("Hello there " + userName);
        System.out.println("Welcome to the world of never ending lies. You can only leave if you solve my simple question.");
        System.out.println("What number between 1 and 10 do I like the most?");

        boolean isGuessed = false; // 标记是否猜对
        int numbs;

        do {
            numbs = in.nextInt(); // 每次循环都重新获取输入
            switch (numbs) {
                case 1:
                    System.out.println("This is not my favorite number.");
                    System.out.println("Please try again traveler, though I shouldn't have to say it.");
                    break;
                case 2:
                    System.out.println("This traveler is indeed my favorite number."); // 终于答对啦!
                    System.out.println("Congratulations! You can leave now, traveler.");
                    isGuessed = true; // 标记为已猜对,结束循环
                    break;
                case 3:
                    System.out.println("Did I tell you about that time in France? WRONG AGAIN!");
                    System.out.println("Please try again traveler, though I shouldn't have to say it.");
                    break;
                case 4:
                    System.out.println("This is definitely not it.");
                    System.out.println("Please try again traveler, though I shouldn't have to say it.");
                    break;
                case 5:
                    System.out.println("Wrong.");
                    System.out.println("Please try again traveler, though I shouldn't have to say it.");
                    break;
                case 6:
                    System.out.println("Wrong again.");
                    System.out.println("Please try again traveler, though I shouldn't have to say it.");
                    break;
                case 7:
                    System.out.println("Haha, you would think of this wouldn't you? W.r.O.n.G");
                    System.out.println("Please try again traveler, though I shouldn't have to say it.");
                    break;
                case 8:
                    System.out.println("Not right at all");
                    System.out.println("Please try again traveler, though I shouldn't have to say it.");
                    break;
                case 9:
                    System.out.println("Who do you think that I are, some girl that you'd meet at a bar? WRONG.");
                    System.out.println("Please try again traveler, though I shouldn't have to say it.");
                    break;
                case 10:
                    System.out.println("You are as naive as you are stupid. WRONG.");
                    System.out.println("Please try again traveler, though I shouldn't have to say it.");
                    break;
                default:
                    System.out.println("That's not even a choice you fool.");
                    System.out.println("Please try again traveler, though I shouldn't have to say it.");
                    break;
            }
        } while (!isGuessed); // 没猜对就继续循环

        in.close(); // 记得关闭Scanner哦
    }
}

关键改动说明

  1. 添加了do-while循环:把输入和switch整个包起来,确保玩家可以重复猜直到答对
  2. 修正了case2的提示:原来的提示会让玩家继续猜,现在改成恭喜并结束循环的内容
  3. 添加了isGuessed标记变量:用来控制循环的终止条件,猜对后设为true,循环就会停下
  4. 关闭了Scanner:这是个好习惯,避免资源泄漏

这样修改后,你的游戏就能正常重复运行直到玩家猜对数字啦!

内容的提问来源于stack exchange,提问作者niegelx4

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 06:31:11