while loop is skipping over my if/else statements - java

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;
}
}

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.

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;
}

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.

How to compare a String with an integer?

How can I compare a string with an int? I am making a Rock-paper-scissors game and how do I turn the string the user enters in to a int so the program can check who had won? Such as if the users enters "rock" the program registers that as 0 and so on?
package rpc;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
/* Random number generator */
Random random = new Random();
/* Scanner object for input */
Scanner scanner = new Scanner(System.in);
/*
* Integer variables to hold the user and computer choice.
* 0 = Rock
* 1 = Paper
* 2 = Scissors
*/
String userChoice;
int computerChoice;
// Showing prompt and user input
System.out.println("Enter move (0 = Rock; 1 = Paper; 2 = Scissors):");
userChoice = scanner.nextLine();
// Checking if userChoice is 0, 1, or 2.
if (!userChoice.equalsIgnoreCase("Scissors") && !userChoice.equalsIgnoreCase("Paper")
&& !userChoice.equalsIgnoreCase("rock")) {
System.out.println("Invalid choice. Ending program.");
// Exit program
Main.main(args);
}
// Generating random computer choice
computerChoice = random.nextInt(3);
// Determining the winner
// If the choices are equal, it's a tie.
if (userChoice == computerChoice) {
if (userChoice == 0) {
System.out.println("Both players chose rock!");
} else if (userChoice == 1) {
System.out.println("Both players chose paper!");
} else {
System.out.println("Both players chose scissors!");
}
// Exit program
System.exit(0);
}
if (userChoice == 0) { // User chooses rock
if (computerChoice == 1) {
System.out.println("You chose rock; Computer chose paper");
System.out.println("Computer wins!");
} else {
System.out.println("You chose rock; Computer chose scissors");
System.out.println("You win!");
}
} else if (userChoice == 1) { // User chooses paper
if (computerChoice == 0) {
System.out.println("You chose paper; Computer chose rock");
System.out.println("You win!");
} else {
System.out.println("You chose paper; Computer chose scissors");
System.out.println("Computer wins!");
}
} else { // User chooses scissors
if (computerChoice == 0) {
System.out.println("You chose scissors; Computer chose rock");
System.out.println("Computer wins!");
} else {
System.out.println("You chose scissors; Computer chose paper");
System.out.println("You win!");
}
}
scanner.close();
}
}
You could use an enum to enumerate the three possible choices:
enum Hand {
ROCK,
PAPER,
SCISSORS;
public static Hand from(String input) {
for (Hand hand : values()) {
if (hand.name().equalsIgnoreCase(input)) {
return hand;
}
}
throw new IllegalArgumentException("Invalid choice: " + input);
}
}
Enums have an intrinsic integer value (that corresponds to the position they were defined at). ROCK.ordinal() will return 0, for example.
Just use pareseInt and convert string to int
For ex :
if(Integer.parseInt(userChoice) == computerChoice)
Make sure that the inputs are not null and formattable to int
edit : change parese to parse
Retrieving a random item from ArrayList
This is not the exact answer to your question (Integer.parseInt(myInt)) but you could try something more readable like this, avoiding the use of unnecessary Integers. And simplifies your code
Generate your arrayList and then pick the random "computer" choice.
List<String> posibilities = Arrays.asList("rock","paper","scissors");
String computerChoice = possibilites.get(Math.random(3));
then do your comparaison ;)
/* Chose the possibilities */
List<String> posibilities = Arrays.asList("rock","paper","scissors");
/* Scanner object for input */
Scanner scanner = new Scanner(System.in);
// Showing prompt and user input
System.out.println("Enter move (0 = Rock; 1 = Paper; 2 = Scissors):");
String userChoice = scanner.nextLine();
userChoice = possibilities.get(Integer.parseInt(userChoice));
// Checking if userChoice is 0, 1, or 2.
if(!possibilities.contains(userChoice)) {
System.out.println("Invalid choice. Ending program.");
// Exit program
Main.main(args);
}
// Generating random computer choice
String computerChoice = possibilites.get(Math.random(3));
// Determining the winner
// If the choices are equal, it's a tie.
if(userChoice.equals(computerChoice)) {
System.out.println("Both players chose " + userChoice);
// Exit program
System.exit(0);
}
System.out.println("You chose " + userChoice + "; Computer chose " + computerChoice);
if(userChoice.equals("rock")) { // User chooses rock
if(computerChoice.equals("paper")) {
System.out.println("Computer wins!");
} else {
System.out.println("You win!");
}
}
else if(userChoice.equals("paper")) { // User chooses paper
if(computerChoice.equals("rock")) {
System.out.println("You win!");
} else {
System.out.println("Computer wins!");
}
} else { // User chooses scissors
if(computerChoice.equals("Scissors")) {
System.out.println("Computer wins!");
} else {
System.out.println("You win!");
}
}
scanner.close();

trouble getting results from rock paper scissors code (java)

so when I run my code, it keeps saying that it is a tie because I think there is a problem in one of my methods.
Here is my whole code:
String userChoice = "";
String computerChoice = "";
System.out.println("Welcome to Rock, Paper, Scissors.");
System.out.println("The rules of the game are Rock breaks Scissors, "
+ "Scissors cut Paper, and Paper covers Rock. In this game, "
+ "the user will play against the computer.");
System.out.println("The legend for this game is: R = rock, P = paper,"
+ " and S = scissors.");
generateUserChoice();
generateComputerChoice();
determiningOutcome(userChoice, computerChoice);
repeatGame(userChoice, computerChoice);
}
public static String generateComputerChoice() {
String computerChoice = "";
int computerIntChoice;
Random generator = new Random();
computerIntChoice = generator.nextInt(3) + 1;
if (computerIntChoice == 1) {
computerChoice = "R";
} else if (computerIntChoice == 2) {
computerChoice = "P";
} else if (computerIntChoice == 3) {
computerChoice = "S";
}
System.out.println("The computer played " + computerChoice);
return computerChoice;
}
public static String generateUserChoice() {
String userChoice;
Scanner input = new Scanner(System.in);
System.out.println("Please Enter your choice:");
userChoice = input.nextLine();
userChoice = userChoice.toUpperCase();
return userChoice;
}
public static void determiningOutcome(String userChoice, String computerChoice) {
if (userChoice.equals(computerChoice)) {
System.out.println("It is a tie!");
} else if (userChoice.equalsIgnoreCase("R") && computerChoice.equalsIgnoreCase("S")) {
System.out.println("Rock beats Scissors, you win!!");
} else if (userChoice.equalsIgnoreCase("P") && computerChoice.equalsIgnoreCase("R")) {
System.out.println("Paper beats Rock, you win!!");
} else if (userChoice.equalsIgnoreCase("S") && computerChoice.equalsIgnoreCase("P")) {
System.out.println("Scissors beats Paper, you win!!");
} else if (computerChoice.equalsIgnoreCase("R") && userChoice.equalsIgnoreCase("S")) {
System.out.println("Rock beats Scissors, you lose.");
} else if (computerChoice.equalsIgnoreCase("P") && userChoice.equalsIgnoreCase("R")) {
System.out.println("Paper beats Rock, you lose.");
} else if (computerChoice.equalsIgnoreCase("S") && userChoice.equalsIgnoreCase("P")) {
System.out.println("Scissors beats Paper, you lose.");
} else {
System.out.println("Sorry, invalid choice.");
}
}
public static int repeatGame(String userChoice, String computerChoice) {
int playAgain;
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play again? 1 = yes and 2 = no");
playAgain = input.nextInt();
if (playAgain == 1) {
System.out.println("Would you like to play again? 1 = yes and 2 = no");
generateUserChoice();
generateComputerChoice();
determiningOutcome(userChoice, computerChoice);
}else {
System.out.println("Thank you for playing!!");
}
return playAgain;
}
}
however I think the problem is in the generateComputerChoice part of my code
Thanks in advance
The problem is that the userChoice and computerChoice is always been empty therefore it always a tie.
You need to assign your userChoice and computerChoice from the return value of the methods.
like this:
userChoice = generateUserChoice();
computerChoice = generateComputerChoice();

Categories