How can I make a while loop only loop once and then ask for user input again? Right now, my while loop goes on forever, but I want it to loop one time, then ask for user input.
@xMisbahx sure. So this is what you while loop should look like:
while(yandn.equals("y")) {
yandn = "";
Simon.add("hello");
Simon.add("helloooooo");
Simon.add("there");
Simon.add("how");
Simon.add("are");
Simon.add("you");
Simon.add("good");
Simon.add("bye");
Simon.add("i am");
Simon.add("leaving");
Simon.add("now");
Simon.add("bye");
Random S = new Random();
System.out.println(Simon.get(S.nextInt(Simon.size())));
System.out.println("Simon Says: would you like to continue?");
System.out.println("y or n");
yandn = scan.nextLine();
}
You'll notice the only difference is the top and bottom of the while loop. What's happening is yandn no longer equals "y" so the while loop will not repeat, but at the end it changes it back so that it repeats after the user says to.
question about while loops
How can I make a while loop only loop once and then ask for user input again? Right now, my while loop goes on forever, but I want it to loop one time, then ask for user input.
while loop
should look like:You'll notice the only difference is the top and bottom of the
while loop
. What's happening isyandn
no longer equals "y" so the while loop will not repeat, but at the end it changes it back so that it repeats after the user says to.