как зациклить переключатель в затмении - PullRequest
0 голосов
/ 09 марта 2019

Я пытался сделать простую игру, в которой вам нужно было бы угадать число от 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;

1 Ответ

0 голосов
/ 09 марта 2019

Вы должны добавить цикл 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.
}
...