#loops #switch-statement
#циклы #switch-инструкция
Вопрос:
Я пытался создать простую игру, в которой вам нужно было бы угадать число от 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:
Вы должны добавить 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.
}