0

The assignment says "using conditionals and a while loop, prompt the user to either ask another question of thank them for using the program, depending on what they decide."

Trying to figure out how to use a while loop to continuously prompt the user to decide if they would like to continue. Wouldn't I need a new scanner each time?

Also, my program stops right after the user enters yes or no in the second scanner. Ahhhhhh.

Here's what I'm working with:

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

}
}

}

  • You need to use the same scanner, since it consumes the "console input stream" (System.in). – Pinkie Swirl Oct 02 '16 at 21:59
  • 4
    `if (YesNo == "no")` -> [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Oct 02 '16 at 22:02

2 Answers2

1

You could move the if-else statements to a switch ( rand ) case 1: break; ... basis. (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html)

To continuously ask the player a question, I would use a

while ( condition )

of

do...while( condition ) (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html)

loop. Here, at the end of every loop, I would ask the player a question and depending on the answer, I would either set the condition to false, thus ending the program, or otherwise continuing on with another loop of the program.

Also you can use the break; statement to end a loop and continue; statement to go to the beginning of a loop.

Hope this helps!

PS. This seems helpful for you (Break DO While Loop Java?) PS2. Use the same scanners

Community
  • 1
  • 1
Karl Johan Vallner
  • 3,980
  • 4
  • 35
  • 46
0

You only need one Scanner and one Random

Example:

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

This code asks the user

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

This line assigns the user input to the string named line and checks if it does not equal "no" (notice the ! at the beginning)

If the first input is always a question, you can use a do-while loop:

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"));
Pinkie Swirl
  • 2,375
  • 1
  • 20
  • 25