需要在循环时编写Magic 8 Ball

时间:2016-10-02 21:55:19

标签: java

作业说"使用条件和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?: ");

}
}

}

2 个答案:

答案 0 :(得分:0)

你可以将if-else语句移动到switch(rand)case 1:break; ......基础(https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

要不断向玩家询问问题,我会使用

while(条件)

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

环。在这里,在每个循环结束时,我会问玩家一个问题,根据答案,我会将条件设置为false,从而结束程序,或者继续使用程序的另一个循环。

你也可以使用 打破; 声明结束循环和 继续; 语句转到循环的开头。

希望这有帮助!

PS。这似乎对你有帮助(Break DO While Loop Java?) PS2。使用相同的扫描仪

答案 1 :(得分:0)

您只需要一个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")

此代码询问用户

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

此行将用户输入分配给名为line的字符串,并检查它是否不等于“no”(注意开头的!)

如果第一个输入总是一个问题,你可以使用do-while循环:

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

          public boolean onQueryTextSubmit(String query) {
               searchView.clearFocus();     // Close keyboard on pressing IME_ACTION_SEARCH option
               Log.d(TAG, "QueryTextSubmit : "+ query);
               //load search query
               return true;
    }

          @Override
          public boolean onQueryTextChange(String newText) {
               Log.d(TAG, "QueryTextChange: "+ newText);
               return false;
          }
});