2016-10-02 76 views
0

該任務說「使用條件和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?: "); 

} 
} 

}

+0

您需要使用相同的掃描儀,因爲它消耗的「控制檯輸入流」(System.in)。 –

+4

if(YesNo ==「no」)' - > [如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java ) – Pshemo

回答

0

你可以在if-else語句移動到一個開關(蘭特)的情況下1:突破; ...的基礎。 (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

要不斷要求玩家一個問題,我會用一個

而(條件)

做......而(條件) (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

循環。在這裏,在每個循環結束時,我會問玩家一個問題,根據答案,我會將條件設置爲false,從而結束程序,或者繼續執行另一個程序循環。

你也可以使用 break; 聲明結束循環並且 繼續; 語句去循環的開始。

希望這會有所幫助!

PS。這似乎對你有幫助(Break DO While Loop Java?) PS2。使用相同的掃描儀

0

你只需要一個Scanner和一個Random

例子:

// We only need one of these 
Scanner reader = new Scanner(System.in); 
Random generator = new Random(); 

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: "); 

while(!reader.nextLine().equals("no")) 
{ 
    // Input was not "no" 
    int rand = generator.nextInt(20) + 1; 

    // If statementns 
    if (rand == 1) 
     // Do stuff... 

    System.out.print("\nWould you like to ask another question?: "); 
} 

// Input was "no" 
System.out.println("Thanks for playing!"); 

該代碼要求用戶

!(reader.nextLine().equals("no") 

此行用戶輸入指定的字符串命名行並檢查它是否不等於「否」(注意開頭!)

如果第一個輸入始終是一個問題,你可以使用一個do-while循環:

reader.nextLine(); // Discard the first input 
do 
{ 
    // Input was not "no" 
    int rand = generator.nextInt(20) + 1; 

    // If statementns 
    if (rand == 1) 
     // Do stuff... 

    System.out.print("\nWould you like to ask another question?: "); 
} while(!reader.nextLine().equals("no"));