if-else Rock Paper Scissors Game - java

I can't see why my program terminates. Example is if I stated the user input is rock, it will sometimes state its a tie or nothing happens/terminates. I tried running bits and pieces of it in different class area but still when I input a value it won't output the results I want in the if/else statements.
I know that the computer choice and user input code is correct. I believe it is the if/else that I am messing up. Correct me if I am wrong..
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int compVal = (int) (3*Math.random()) +1;
char compChoice;
if (compVal == 1) {
compChoice = 'R';
} else if (compVal == 2) {
compChoice = 'P';
} else {
compChoice = 'S';
}
System.out.println("Rock, Paper, Scissors-Enter a choice R/P/S: ");
String mine = keyboard.nextLine();
char myChoice = mine.charAt(0);
myChoice = Character.toUpperCase(myChoice);
if (myChoice == (compChoice)) {
System.out.println("We both chose the same item-try again");
} else if (myChoice == ('R')) {
if (compChoice == 'P') {
System.out.println("I chose Paper and you chose Rock: Paper covers rock, so I win");
}
} else if (myChoice == ('R')) {
if (compChoice == ('S')) {
System.out.println("I chose Scissors and you choise Rock: Rock breaks Scissors, so you win");
}
} else if (myChoice == ('P')) {
if (compChoice == ('S')) {
System.out.println("I chose Scissors and you chose Paper: Scissors cuts Paper, so I win");
}
} else if (myChoice == ('P')) {
if (compChoice == ('R')) {
System.out.println("I chose Rock and you chose Paper: Paper covers Rock, so you win");
}
} else if (myChoice == ('S')) {
if (compChoice == ('P')) {
System.out.println("I chose Paper and you chose Scissors: Scissors cuts Paper, so you win");
}
} else if (myChoice == ('S')) {
if (compChoice == ('R')) {
System.out.println("I chose Rock and you chose Scissors: Rock breaks Scissors, so I win");
}
}
}

You have multiple if/else statements which can never be executed.
Consider this code
if (myChoice == ('R')) {
if (compChoice == 'P') {
System.out.println("I chose Paper and you chose Rock: Paper covers rock, so I win");
}
} else if (myChoice == ('R')) { // this will ONLY execute if the previous if statement is false.
Every second if clause cannot execute because you have already checked for that condition. i.e. the choise cannot be R because if it was it would have executed the previous block. The simplest solution is to delete the duplicates as you don't need them.
} else if (myChoice == ('R')) {
if (compChoice == 'P') {
System.out.println("I chose Paper and you chose Rock: Paper covers rock, so I win");
}
if (compChoice == ('S')) {
System.out.println("I chose Scissors and you choise Rock: Rock breaks Scissors, so you win");
}
or even
} else if (myChoice == 'R') {
if (compChoice == 'P')
System.out.println("I chose Paper and you chose Rock: Paper covers rock, so I win");
else
System.out.println("I chose Scissors and you choise Rock: Rock breaks Scissors, so you win");
} else

Related

How do I count the playerwins for my code

Aye, so I have this assignment to make a rock, paper, scissor game. I did mostly everything right (maybe), but I can't figure out how to count the playerwins at the end when stopping a game of rock, paper, scissors. It is the one thing I am missing when running and stopping the code.
Playerwins are at the very bottom and the very top. The code is long as hell, but im new at coding and don't know how to make it less redundant.
import java.util.*;
import java.io.*;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner dodongo = new Scanner(System.in);
Random hamster = new Random();
System.out.println("Rock, Paper, Scissors!");
System.out.println("Press 1 for Rock, 2 for Paper, or 3 for Scissors");
while (!dodongo.hasNextInt(4)) {
System.out.println("Only numbers 1-3!");
System.out.println("Press 1 for Rock, 2 for Paper, or 3 for Scissors");
dodongo.next();
}
int player = dodongo.nextInt();
int rock = 1;
int paper = 2;
int scissors = 3;
int playerwins = 0;
System.out.println("YOU: Rock, Paper, Scissors! ");
System.out.println();
if ((player >= 0) && (player <= 1)) {
System.out.println("Rock!");
} else if ((player >= 1) && (player <= 2)) {
System.out.println("Paper!");
} else if ((player >= 2) && (player <= 3)) {
System.out.println("Scissors!");
}
System.out.println("Computer is playing...");
int shoot = -1;
System.out.println("COM: Rock, Paper, Scissors!");
do {
int keyblade = hamster.nextInt(3) + 1;
if ((keyblade >= 0) && (keyblade <= 1)) {
System.out.println("Rock!");
} else if ((keyblade >= 1) && (keyblade <= 2)) {
System.out.println("Paper!");
} else if ((keyblade >= 2) && (keyblade <= 3)) {
System.out.println("Scissors!");
}
shoot++;
if ((player == rock) && (keyblade == paper)) {
System.out.println("COM Win!");
System.out.println("You Lose! Paper beats rock, because a piece of paper can cover a rock!");
} else if ((player == rock) && (keyblade == scissors)) {
System.out.println("You Win! Rock beats scissors, because a rock can break a pair of scissors!");
} else if ((player == paper) && (keyblade == rock)) {
System.out.println("You Win! Paper beats rock, because a piece of paper can cover a rock!");
} else if ((player == paper) && (keyblade == scissors)) {
System.out.println("COM Win!");
System.out.println("You Lose! Scissors beats paper, because scissors can cut paper!");
} else if ((player == scissors) && (keyblade == rock)) {
System.out.println("COM Win!");
System.out.println("You Lose! Rock beats scissors, because a rock can break a pair of scissors!");
} else if ((player == scissors) && (keyblade == paper)) {
System.out.println("You Win! Scissors beats paper, because scissors can cut paper!");
} else {
System.out.println("Tie!");
}
playerwins++;
} while (shoot != 0);
System.out.println("Want to play again?(Press Y or N)");
String tryagain = dodongo.next();
if (tryagain.equalsIgnoreCase("Y")) {
main(null);
} else {
System.out.println("Adios...");
System.out.println("Total Wins: " + playerwins);
}
System.out.println();
}
}
I broke your code into methods. For the most part, this just organized the code a little better. I was able to use the writeInput method twice, so I also slightly reduced the duplication.
Once I created methods, the code in the main method was reduced enough that I could make a game loop using a do-while loop and remove the recursion.
I also corrected the playerwins count so it increments only when the player wins. The corrected code is in the compareInputs method.
Here's your code after adding methods.
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner dodongo = new Scanner(System.in);
Random hamster = new Random();
int playerwins = 0;
String tryagain;
do {
int player = readPlayerInput(dodongo);;
System.out.println("YOU: Rock, Paper, Scissors! ");
System.out.println();
writeInput(player);
System.out.println("Computer is playing...");
System.out.println("COM: Rock, Paper, Scissors!");
int keyblade = hamster.nextInt(3) + 1;
writeInput(keyblade);
playerwins += compareInputs(player, keyblade);
System.out.println("Want to play again?(Press Y or N)");
tryagain = dodongo.next();
} while (tryagain.equalsIgnoreCase("Y"));
System.out.println("Adios...");
System.out.println("Total Wins: " + playerwins);
System.out.println();
}
private static int readPlayerInput(Scanner dodongo) {
System.out.println("Rock, Paper, Scissors!");
System.out.println("Press 1 for Rock, 2 for Paper, or 3 for Scissors");
while (!dodongo.hasNextInt(4)) {
System.out.println("Only numbers 1-3!");
System.out.println("Press 1 for Rock, 2 for Paper, or 3 for Scissors");
dodongo.next();
}
return dodongo.nextInt();
}
private static void writeInput(int input) {
if ((input >= 0) && (input <= 1)) {
System.out.println("Rock!");
} else if ((input >= 1) && (input <= 2)) {
System.out.println("Paper!");
} else if ((input >= 2) && (input <= 3)) {
System.out.println("Scissors!");
}
}
private static int compareInputs(int player, int keyblade) {
int rock = 1;
int paper = 2;
int scissors = 3;
int playerWins = 0;
if ((player == rock) && (keyblade == paper)) {
System.out.println("COM Win!");
System.out.println("You Lose! Paper beats rock, because a "
+ "piece of paper can cover a rock!");
} else if ((player == rock) && (keyblade == scissors)) {
System.out.println("You Win! Rock beats scissors, because a "
+ "rock can break a pair of scissors!");
playerWins = 1;
} else if ((player == paper) && (keyblade == rock)) {
System.out.println("You Win! Paper beats rock, because a "
+ "piece of paper can cover a rock!");
playerWins = 1;
} else if ((player == paper) && (keyblade == scissors)) {
System.out.println("COM Win!");
System.out.println("You Lose! Scissors beats paper, because "
+ "scissors can cut paper!");
} else if ((player == scissors) && (keyblade == rock)) {
System.out.println("COM Win!");
System.out.println("You Lose! Rock beats scissors, because a "
+ "rock can break a pair of scissors!");
} else if ((player == scissors) && (keyblade == paper)) {
System.out.println("You Win! Scissors beats paper, because "
+ "scissors can cut paper!");
playerWins = 1;
} else {
System.out.println("Tie!");
}
return playerWins;
}
}
The first thing you need when you program any game is that you need a game loop, which runs the game until the user exits it. Interestingly, you opt for recursively calling main, which is also possible (although it could theoretically overload stack, which probably won't happen in this simple case but could be a real problem in a game that loops many times per second for a long time).
So the easiest fix would be to move the playerwins variable out of the main function (then you need to make it static as well) but the correct code that builds better habits for later work would be to use another while loop instead of recursively calling main.
Normally, beginners start with iterative code and discover recursion later, so it is nice that you discover it so early, but unfortunately that is not the correct case for it, but keep it in your pocket for other occasions.

while loop is skipping over my if/else statements

I have this while loop of a Rock Paper Scissors game and I have if/else if statements that tell you which player won, but the while loop is skipping it and going straight to the end of the loop where it asks if you want to play another game. How would I be able to change this?
Scanner scan = new Scanner(System.in);
String play = "";
System.out.print("Please enter Play if you want to play the game or anything else to Stop");
play = scan.nextLine();
while (play.equalsIgnoreCase("play")) {
System.out.println("Game " + gameCount + " Rock, Paper, Scissors - Play!");
System.out.print("Choose your weapon [R]ock, [P]aper, or [S]cissors: ");
String rps = scan.nextLine();
while (rps.equals('R') || rps.equals('P') || rps.equals('S')) {
System.out.println("You chose: " + rps);
}
int rand = (int)(Math.random() * 3);
String myMove = "";
if(rand == 0) {
myMove = "Rock";
}
else if(rand == 1) {
myMove = "Paper";
}
else {
myMove = "Scissors";
}
System.out.println("I chose: " + myMove);
if(rps.equals(myMove)) {
System.out.println("Tie!");
tieCount++;
}
else if(rps.equals('P') && myMove.equals("Scissors")) {
System.out.println("Scissors beats paper, a win for me!");
myCount++;
}
else if(rps.equals('S') && myMove.equals("Rock")) {
System.out.println("Rock beats scissors, a win for me!");
myCount++;
}
else if(rps.equals('R') && myMove.equals("Paper")) {
System.out.println("Paper beats rock, a win for me!");
myCount++;
}
else if(rps.equals('S') && myMove.equals("Paper")) {
System.out.println("Scissors beats paper, a win for you!");
userCount++;
}
else if(rps.equals('R') && myMove.equals("Scissors")) {
System.out.println("Rock beats scissors, a win for you!");
userCount++;
}
else if(rps.equals('S') && myMove.equals("Paper")) {
System.out.println("Paper beats rock, a win for you!");
userCount++;
}
gameCount++;
System.out.println("Please enter Play if you want to play the game again or anything else to Stop.");
play = scan.nextLine();
}
'''
Try this out
while (true) {
System.out.println("Insert question code:");
String question = scanner.nextLine();
if(question.equals("quit")){
break;
}
System.out.println("Insert answer code:");
String answer = scanner.nextLine();
if(answer.equals("quit")){
break;
}
}

How can I assign user input (words) an integer value to be used in the code

I'm attempting to create a game of "Rock Paper Scissors Lizard Spock", and want the user to be able to put in either the integers assigned to the variables (Rock=0, Paper=1, Scissors=2, Lizard=3 and Spock=4) and also be able to enter the words "Rock", "Paper", "Scissors", "Lizard" or "Spock". Could you help me include a part of the code where I can assign the string inputs to integers. I also do not want to change the main framework of the code. I also know that the website says not to paste the entire file, but I cannot think of another way to show my problem. Please note that I have been using a website called SoloLearn. Any help would be greatly appreciated.
import java.util.Scanner;
public class RockPaperScissorsLizardSpock {
final static int ROCK = 0;
final static int PAPER = 1;
final static int SCISSORS = 2;
final static int LIZARD = 3;
final static int SPOCK = 4;
public static void main(String[] args) {
double r = Math.random();
int computerChoice = (int)(3.0 * r);
Scanner input = new Scanner(System.in);
System.out.print("Enter 0 for Rock, 1 for Paper, 2 for Scissors, 3 for Lizard, 4 for Spock: ");
int playerChoice = input.nextInt();
System.out.println(computerChoice);
int playerChoice = 0;
switch (playerChoice) {
case "Rock":
playerChoice = 0;
break;
case "Paper":
playerChoice = 1;
break;
case "Scissors":
playerChoice = 2;
case "Lizard":
playerChoice = 3;
case "Spock":
playerChoice = 4;
if (computerChoice == playerChoice) {
System.out.println("Tie");
}
else if (computerChoice == ROCK && playerChoice == SCISSORS) {
System.out.println("I chose Rock,You chose Scissors, Rock crushes Scissors, You lose.");
}
else if (computerChoice == SCISSORS && playerChoice == PAPER) {
System.out.println("I chose Scissors, You chose Paper, Scissors cut Paper, You lose.");
}
else if (computerChoice == PAPER && playerChoice == ROCK) {
System.out.println("I chose Paper,You chose Rock, Paper covers Rock, You lose.");
}
else if (computerChoice == LIZARD && playerChoice == PAPER) {
System.out.println("I chose Lizard, You chose Paper, Lizard eats Paper, You lose.");
}
else if (computerChoice == SPOCK && playerChoice == SCISSORS) {
System.out.println("I chose Spock, You chose Scissors, Spock smashes Scissors, You lose.");
}
else if (computerChoice == ROCK && playerChoice == LIZARD) {
System.out.println("I chose Rock, You chose Lizard, Rock crushes Lizard, You lose.");
}
else if (computerChoice == SCISSORS && playerChoice == LIZARD) {
System.out.println("I chose Scissors, You chose Lizard, Scissors decapitates Lizard, You lose.");
}
else if (computerChoice == SPOCK && playerChoice == PAPER) {
System.out.println("I chose Spock, You chose Paper, Paper disproves Spock, You lose.");
}
else if (computerChoice == SPOCK && playerChoice == ROCK) {
System.out.println("I chose Spock, You chose Rock, Spock vaporizes Rock, You lose.");
}
else if (computerChoice == SCISSORS && playerChoice == ROCK) {
System.out.println("I chose Scissors, You chose Rock, Rock crushes Scissors, You win.");
}
else if (computerChoice == PAPER && playerChoice == SCISSORS) {
System.out.println("I chose Paper, You chose Scissors, Scissors cut Paper, You win.");
}
else if (computerChoice == ROCK && playerChoice == PAPER) {
System.out.println("I chose Rock, You chose Paper, Paper covers Rock,You win.");
}
else if (computerChoice == PAPER && playerChoice == LIZARD) {
System.out.println("I chose Paper, You chose Lizard, Lizard eats Paper, You win.");
}
else if (computerChoice == SCISSORS && playerChoice == SPOCK) {
System.out.println("I chose Scissors, You chose Spock, Spock smashes Scissors, You win.");
}
else if (computerChoice == LIZARD && playerChoice == ROCK) {
System.out.println("I chose Lizard, You chose Rock, Rock crushes Lizard, You win.");
}
else if (computerChoice == LIZARD && playerChoice == SCISSORS) {
System.out.println("I chose Lizard, You chose Scissors, Scissors decapitates Lizard, win.");
}
else if (computerChoice == PAPER && playerChoice == SPOCK) {
System.out.println("I chose Paper, You chose Spock, Paper disproves Spock, You win.");
}
else if (computerChoice == ROCK && playerChoice == SPOCK) {
System.out.println("I chose Rock, You chose Spock, Spock vaporizes Rock, You win.");
}else{
System.out.println("Error");
}
}
}
}
If you want that your user can input 2 Values for 1 condition you can convert the value from playerChoice to String for a temp String and then make a switch case for it like the following:
String tempString = Integer.toString(playerChoice);
switch (tempString) {
case "0":
case "Rock":
playerChoice = 0;
break;
case "1":
case "Paper":
playerChoice = 1;
break;
case "2":
case "Scissors":
playerChoice = 2;
break;
case "3":
case "Lizard":
playerChoice = 3;
break;
case "4":
case "Spock":
playerChoice = 4;
break;
}
You could put your allowed inputs in a map, key being the text and value being the associated number:
private static final Map<String, Integer> ALLOWED_INPUTS = Collections.unmodifiableMap(Map.of(
"ROCK", 0,
"PAPER", 1,
"SCISSORS", 2,
"LIZARD", 3,
"SPOCK", 4
));
Then, in your main function, check if the map contains a key that corresponds to the user input, if so get the corresponding value. If not, check if the user entered a number. Keep looping while the resulting number either way isn't in the map's values.
Scanner in = new Scanner(System.in);
int playerChoice = -1;
String input;
while(!ALLOWED_INPUTS.containsValue(playerChoice)) {
input = in.nextLine();
if (ALLOWED_INPUTS.containsKey(input))
playerChoice = ALLOWED_INPUTS.get(input);
else {
try {
playerChoice = Integer.parseInt(input);
} catch(NumberFormatException e) {}
}
}
System.out.println("You choosed choice number " + playerChoice);
Assuming : Either Words or Integers are entered as inputs.
Steps for this solution:
Always Entering String Input: String choice = input.next();
Checking if choice contains only digits, if yes,convert this to Integer else don't do anything.
if entered input is words, it will go to switch first,then set playerChoice and run properly.
if entered input is digit, it will assign converted integer to playerChoice first, then it will go to switch but as no default exists, it will run properly.
String choice = input.next();
if (choice.matches("\\d+"))
playerChoice = Integer.parseInt(choice);
switch (choice.toUpperCase()) {
case "ROCK":
playerChoice = 0;
break;
case "PAPER":
playerChoice = 1;
break;
case "SCISSORS":
playerChoice = 2;
break;
case "LIZARD":
playerChoice = 3;
break;
case "SPOCK":
playerChoice = 4;
break;
}

Rock Paper Scissors does not work as expected [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm doing this Rock, Paper, Scissors Java program for an introduction to programming class and for some reason, the program is automatically terminating after the first user input.
I have a feeling it has to do with the
int randomNumber = rnd.nextInt(3) + 1;
statement but I'm not sure what I'm doing wrong here.
public static void main(String[] args) {
char userChar;
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
// Intro/directions/prompting for user input
System.out.println("Welcome to Rock, Paper, Scissors by Rancid!");
System.out.println("Choose R for Rock, P for Paper, S for Scissors, or Q to Quit, and press Enter: ");
userChar = sc.next().charAt(0);
// Prompting computer to generate a random number
int randomNumber = rnd.nextInt(3) + 1;
// If computer generates 1 (Rock)
if (randomNumber == 1) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock vs. Rock! It's a tie!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper covers Rock, you win!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Rock breaks Scissors, you lose!");
}
// If computer generates 2 (Paper)
if (randomNumber == 2) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Paper covers Rock, you lose!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper vs. Paper! It's a tie!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors cuts Paper, you win!");
}
// If computer generates 3 (Scissors)
if (randomNumber == 3) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock breaks Scissors, you win!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Scissors cuts Paper, you lose!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors vs. Scissors! It's a tie!");
}
// If player chooses to quit
if (userChar == 'q' || userChar == 'Q') {
System.out.println("Player chose to quit. Goodbye!");
}
// If player types an invalid character
else {
System.out.println("Invalid input! Please enter a valid character.");
}
}
}
}
}
}
Welcome to stackoverflow.
It seems like you're not using any loop inside your main method, so your program simply closes after it executed the last statement.
You want to add something like:
while (!"q".equals(userChar) && !"Q".equals(userChar)) {
System.out.println("Choose R for Rock, P for Paper, S for Scissors, or Q to Quit, and press Enter: ");
userChar = sc.next().charAt(0);
// Add your code where you check the randomNumber and the userChar here
}
Moreover you want to check your curly braces as the comments already state.

How do I solve the loop issues? It is the place of the brackets but I cannot figure out where they should be placed

I am trying to code Rock, paper, scissors, lizard, Spock. My code is attached and the loops keep repeating. I believe the curly braces are placed wrong but I cannot figure out where without getting an error message.
public class RockPaperScissors_v2 {
public static void main(String[] args) {
//scanner
Scanner inputReader = new Scanner(System.in);
//randomizer for comp
Random rand = new Random();
//variables
int rock = 1;
int paper = 2;
int scissors = 3;
int lizard = 4;
int spock = 5;
int round;
int choice;
int compChoice = 1;
int countOfWins = 0;
int countOfLosses = 0;
int countOfTies = 0;
//ask how many rounds to play; max 10 --> if more than 10, print error message and quit.
do {
System.out.println("How many rounds do you want to play?");
//output
round = inputReader.nextInt();
if (round < 1 || round > 10) {
System.out.println("Error Message: Please type a number between 1 and 10.");
return;
}
} while (round != 0);
System.out.println("Please select a number from the follow:");
System.out.println("1) Rock"
+ "\n 2) Paper"
+ "\n 3) Scissors"
+ "\n 4) Lizard"
+ "\n 5) Spock");
compChoice = rand.nextInt(5 - 2 + 2) + 1;
choice = inputReader.nextInt();
//if statements for all scenarios
if (compChoice == 1) {
if (choice == 1) {
System.out.println("Tie game.");
countOfTies++;
} else if (choice == 2) {
System.out.println("Rock gets covered by paper. You win!");
countOfWins++;
} else if (choice == 3) {
System.out.println("Rock crushes scissors. You lose.");
countOfLosses++;
} else if (choice == 4) {
System.out.println("Rock crushes lizard. You lose.");
countOfLosses++;
} else if (choice == 5) {
System.out.println("Spock vaporizes rock. You win!");
countOfWins++;
}
} else if (compChoice == 2) {
if (choice == 1) {
System.out.println("Paper covers rock. You lose.");
countOfLosses++;
} else if (choice == 2) {
System.out.println("Tie game.");
countOfTies++;
} else if (choice == 3) {
System.out.println("Scissors cuts paper. You win!");
countOfWins++;
} else if (choice == 4) {
System.out.println("Lizard eats paper. You win!");
countOfWins++;
} else if (choice == 5) {
System.out.println("Paper disproves Spock. You win!");
countOfWins++;
}
} else if (compChoice == 3) {
if (choice == 1) {
System.out.println("Rock crushes scissors. You lose.");
countOfLosses++;
} else if (choice == 2) {
System.out.println("Scissors cuts paper. You lose.");
countOfLosses++;
} else if (choice == 3) {
System.out.println("Tie game.");
countOfTies++;
} else if (choice == 4) {
System.out.println("Scissors decapitates lizard. You lose.");
countOfLosses++;
} else if (choice == 5) {
System.out.println("Spock smashes scissors. You win!");
countOfWins++;
}
} else if (compChoice == 4) {
if (choice == 1) {
System.out.println("Rock crushes lizard. You win!");
countOfWins++;
} else if (choice == 2) {
System.out.println("Lizard eats paper. You lose.");
countOfLosses++;
} else if (choice == 3) {
System.out.println("Scissors decapitates lizard. You win!");
countOfWins++;
} else if (choice == 4) {
System.out.println("Tie game.");
countOfTies++;
} else if (choice == 5) {
System.out.println("Lizard poisons Spock. You lose.");
countOfLosses++;
}
} else if (compChoice == 5) {
if (choice == 1) {
System.out.println("Rock crushes lizard. You lose.");
countOfLosses++;
} else if (choice == 2) {
System.out.println("Lizard eats paper. You win!");
countOfWins++;
} else if (choice == 3) {
System.out.println("Scissors decapitates lizard. You lose.");
countOfLosses++;
} else if (choice == 4) {
System.out.println("Lizard poisons Spock. You win!");
countOfWins++;
} else if (choice == 5) {
System.out.println("Tie game.");
countOfTies++;
}
}
//print out wins, losses, and ties
System.out.println("Number of Wins:" + countOfWins);
System.out.println("Number of Losses: " + countOfLosses);
System.out.println("Number of Ties: " + countOfTies);
//ask user if wants to play again
System.out.println("Do you want to play again (Y/N)?");
String playAgain = inputReader.next();
if (playAgain.equals("Y")) {
} else {
System.out.println("Thank you for playing.");
System.exit(0);
}
}
}
With while (round != 0); the loop will exit only when you are inserting 0, which seems to be wrong and also will never happen since in that case you are closing the program with return.
I suggest to remove the return and to change the while condition with while (round < 1 || round > 10);, which will terminate the loop only when a value between 1 and 10 is inserted.

Categories