Here’s my code so far (well, the while loop):
public class Lab10d
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
char response = 0;
//add in a do while loop after you get the basics up and running
String player = "";
out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
//read in the player value
player = keyboard.next();
RockPaperScissors game = new RockPaperScissors(player);
game.setPlayers(player);
out.println(game);
while(response == ('y'))
{
out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
player = keyboard.next();
game.setPlayers(player);
//game.determineWinner();
out.println(game);
out.println();
//
}
out.println("would you like to play again? (y/n):: ");
String resp = keyboard.next();
response = resp.charAt(0);
}
}
it’s supposed to run the code additional times until an n is inputted
When I input a y, it is supposed to re-run the code but doesn’t
Your
whileloop ends before you ask if they want to play again.Change the loop to:
There is another problem:
responseis not set to ‘y’ before the loop is started. It will not do anything in the loop at all. Use ado { ... } while (response == 'y')loop instead.A do-while will execute the code once, then check the condition and keep executing if it is
true. A while loop will just check the condition, and keep executing while it’strue.EDIT: I put together some code for you:
Output: