【发布时间】: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;
【问题讨论】: