【问题标题】:how to loop a switch in eclipse如何在eclipse中循环开关
【发布时间】:2019-03-08 22:26:39
【问题描述】:

我正在尝试制作一个简单的游戏,您必须在其中猜测 1 到 10 之间的数字,而我希望它会重复进行,直到您猜出正确的数字为止。我为此使用了一个开关,但我还很新,所以我不一定知道如何循环开关。我看过教程,但没有得到任何结果。

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?");
    int numbs;
    numbs = in.nextInt(); // get numbers
    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."); // this is the right number
        System.out.println("Please try again traveler, though I shouldn't have to say it.");
        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;

【问题讨论】:

    标签: loops switch-statement


    【解决方案1】:

    您应该在nextInt 行的顶部添加一个while 循环,条件是只有在值正确时才会退出循环。

    查看while 循环语句。

    举个例子:

    numbs = 0;
    while(numbs != 2){
    System.out.println("Guess the number");
    numbs = in.nexInt();
    //Switch statement here. When the loop gets to the top again, it'll see that number is 2, and hence will break from the loop.
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-24
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      相关资源
      最近更新 更多