如何在eclipse中循环切换

问题描述 投票:0回答:1

我试图做一个简单的游戏,你必须猜测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个回答
0
投票

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

看看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.
}
© www.soinside.com 2019 - 2024. All rights reserved.