in this code is playing Rock Paper Scissors, between the computer and the user. My code is all working great, however, I'm trying to think of a better way to make it ask the user if they would want to play again. If yes, then it would start the program again, if no, then it would stop. My "yes" seems to work but the no will stop and not go through all the way. Any suggestions or tips on how to do this? I will trying to incorporate a different while loop, but wasn't working. Would a do loop be good for this scenario? Thanks!
//import scanner import java.util.Scanner; import java.util.*; //declare variables and main methods class Rock { Scanner scan = new Scanner(System.in); Random generator = new Random(); String response, name; char choice; int rounds, computerChoice, userScore, computerScore; boolean playIntro = true; boolean playGame = true; //this method will run the entire progrma public void playRPS(){ //while loop for beginning of game while(playIntro){ System.out.println("This is a game of Rock Paper Scissors!"); System.out.println("Please enter your name: "); name = scan.nextLine(); //while loop for the actual part of the game while(playGame){ System.out.println("Type R (Rock), P (Paper), or S (Scissors): "); choice = scan.nextLine().charAt(0); computerChoice = generator.nextInt(3)+1; //using switch and case for each choice switch (choice){ //case for Rock case 'R': if(computerChoice==1){ System.out.println("Tie between you and the computer! Go again."); break; } else{ if(computerChoice==2){ System.out.println("The computer beat you this round"); computerScore++; break; } else{ System.out.println("You won this round"); userScore++; break; } } //case for Paper case 'P': if(computerChoice==2){ System.out.println("Tie between you and the computer! Go again."); break; } else{ if(computerChoice==3){ System.out.println("The computer beat you this round"); computerScore++; break; } else{ System.out.println("You won this round"); userScore++; break; } } //case for Scissors case 'S': if(computerChoice==3){ System.out.println("Tie between you and the computer! Go again."); break; } else{ if(computerChoice==1){ System.out.println("The computer beat you this round"); computerScore++; break; } else{ System.out.println("You won this round"); userScore++; break; } } } System.out.println("You have "+userScore+" points and the computer has "+computerScore+" points"); if (userScore==5){ System.out.println("\nOut of 5 rounds, You beat the computer!"); playGame = false; } else if (computerScore==5){ System.out.println("\nOut of 5 rounds, The computer beat you."); playGame = false; } } askUser(); } } public void askUser(){ System.out.println("\nDo you want to play this Rock Paper Scissors again? Type yes: "); response = scan.nextLine(); if (response.equalsIgnoreCase("yes")){ playGame = true; userScore=0; computerScore=0; } else{ playGame = false; scan.nextLine(); } } public static void main() { Rock prog = new Rock(); prog.playRPS(); } }
scan.nextLine();when the user does not enteryes?