該任務說「使用條件和while循環,提示用戶要麼提出另一個問題,要感謝他們使用該程序,這取決於他們的決定。」需要編寫Magic 8 Ball while循環
試圖弄清楚如何使用while循環來持續提示用戶決定是否要繼續。我每次都不需要新的掃描儀嗎?
此外,我的程序在用戶在第二個掃描儀中輸入yes或no後停止。 AHHHHHH。
這裏就是我的工作:
System.out.println("Hello!\nNeed to make a decision?\nWant to know about the future?\nType in a yes or no question and press Enter: ");
Scanner reader = new Scanner(System.in);
String question = reader.nextLine();
System.out.println();
Random generator = new Random();
int rand = generator.nextInt(20) + 1;
if (rand == 1)
{ System.out.println("It is certain.");}
if (rand ==2)
{System.out.println("It is decidedly so."); }
if (rand ==3)
{System.out.println("Without a doubt."); }
if (rand ==4)
{ System.out.println("Yes, definitely.");}
if (rand ==5)
{System.out.println("You may rely on it.");}
if (rand == 6)
{System.out.println("As I see it, yes."); }
if (rand == 7)
{System.out.println("Most likely.");}
if (rand == 8)
{System.out.println("The outlook is good");}
if (rand == 9)
{System.out.println("Yes.");}
if (rand == 10)
{System.out.println("Signs point to yes.");}
if (rand == 11)
{System.out.println("Reply hazy.");}
if (rand == 12)
{System.out.println("Ask again later.");}
if (rand == 13)
{System.out.println("Better not tell you now.");}
if (rand ==14)
{System.out.println("Cannot predict now.");}
if (rand ==15)
{System.out.println("Concentrate and ask again.");}
if (rand == 16)
{System.out.println("Don't count on it.");}
if (rand == 17)
{System.out.println("My reply is no.");}
if (rand ==18)
{System.out.println("My sources say no.");}
if (rand == 19)
{System.out.println("Outlook does not look so good.");}
if (rand == 20)
{System.out.println("Very doubtful.");}
System.out.print("\nWould you like to ask another question?: ");
Scanner reader2 = new Scanner(System.in);
String YesNo = reader2.nextLine();
if (YesNo == "no") {
System.out.println("Thanks for playing!"); }
while (YesNo == "yes") {
System.out.println("Enter your question: ");
Scanner reader3 = new Scanner(System.in);
String question2 = reader3.nextLine();
Random generator2 = new Random();
int rand2 = generator.nextInt(20) + 1;
System.out.println();
if (rand2 == 1)
{ System.out.println("It is certain.");}
if (rand2 ==2)
{System.out.println("It is decidedly so."); }
if (rand2 ==3)
{System.out.println("Without a doubt."); }
if (rand2 ==4)
{ System.out.println("Yes, definitely.");}
if (rand2 ==5)
{System.out.println("You may rely on it.");}
if (rand2 == 6)
{System.out.println("As I see it, yes."); }
if (rand2 == 7)
{System.out.println("Most likely.");}
if (rand2 == 8)
{System.out.println("The outlook is good");}
if (rand2 == 9)
{System.out.println("Yes.");}
if (rand2 == 10)
{System.out.println("Signs point to yes.");}
if (rand2 == 11)
{System.out.println("Reply hazy.");}
if (rand2 == 12)
{System.out.println("Ask again later.");}
if (rand2 == 13)
{System.out.println("Better not tell you now.");}
if (rand2 ==14)
{System.out.println("Cannot predict now.");}
if (rand2 ==15)
{System.out.println("Concentrate and ask again.");}
if (rand2 == 16)
{System.out.println("Don't count on it.");}
if (rand2 == 17)
{System.out.println("My reply is no.");}
if (rand2 ==18)
{System.out.println("My sources say no.");}
if (rand2 == 19)
{System.out.println("Outlook does not look so good.");}
if (rand2 == 20)
{System.out.println("Very doubtful.");}
System.out.print("\nWould you like to ask another question?: ");
}
}
}
您需要使用相同的掃描儀,因爲它消耗的「控制檯輸入流」(System.in)。 –
if(YesNo ==「no」)' - > [如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java ) – Pshemo