I was tasked to create a java program that resembles rock, paper, scissors. I have written what I thought would work below. However, only when the user selects R or r (for rock) does it actually work properly. If the user selects s or p (scissors or paper) the code will completely break or give multiple answers. I have looked through the code and can't seem to find out why it's not working properly.
Also any suggestions on how to better write the switch portion of the code would be appreciated. I have a feeling the way I wrote it is not the proper way.
import java.util.Scanner;
public class Project_2 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int compVal = (int) (3 * Math.random()) + 1;
String compActual = "";
System.out.println("Welcome to Rock, Paper, Scissors!");
System.out.print("Enter r for rock, p for paper, or s for scissors: ");
String userOriginal = keyboard.nextLine();
userOriginal = (userOriginal.toUpperCase());
switch (userOriginal) {
case "r":
userOriginal = userOriginal;
break;
case "R":
userOriginal = userOriginal;
break;
case "p":
userOriginal = userOriginal;
break;
case "P":
userOriginal = userOriginal;
break;
case "s":
userOriginal = userOriginal;
break;
case "S":
userOriginal = userOriginal;
break;
default:
System.out.println("Invalid input, please try again!");
System.exit(1); // This will exit the program if invalid input is
// given. The 1 signifies that it ended with an
// error.
}
if (compVal == 1) {
compActual = "R";
} else if (compVal == 2) {
compActual = "P";
} else if (compVal == 3) {
compActual = "S";
}
if (compActual.equals(userOriginal)) {
System.out.println("It was a tie!");
} else if (compActual.equals("R"))
if (userOriginal.equals("S")) {
System.out.println("You played Scissors and I chose Rock: Rock crushes Scissors so I win this time!");
}
if (userOriginal.equals("P")) {
System.out.println("You played Paper and I chose Rock: Paper covers Rock so you win this time!");
} else if (compActual.equals("S"))
if (userOriginal.equals("R")) {
System.out.println("You played Rock and I chose Sciccors: Rock crushes Scissors so you win this time");
}
if (userOriginal.equals("P")) {
System.out.println("You played Paper and I chose Scissors: Paper is cut by Scissors so I win this time!");
} else if (compActual.equals("P"))
if (userOriginal.equals("R")) {
System.out.println("Your played Rock and I chose Paper: Paper covers rock so I win this time!");
}
if (userOriginal.equals("S")) {
System.out.println("You played Scissors and I chose Paper: Scissors cuts Paper so you win this time!");
}
}
}
As suggested, you should clean up your code a little bit. Why not use numerical values instead of Strings for comparsion, like this:
int compVal = (int) (3 * Math.random()) + 1;
int userOriginal = 0;
String userInput = (keyboard.nextLine().toUpperCase());
switch (userInput) {
case "R":
userOriginal = 1;
break;
case "P":
userOriginal = 2;
break;
case "S":
userOriginal = 3;
break;
default:
System.out.println("Invalid input, please try again!");
System.exit(1); // This will exit the program
}
After that you can compare the user provided value with the generated one:
if (compVal == userOriginal) {
System.out.println("It was a tie!");
} else
{
//R
if (compVal == 1)
{
if (userOriginal == 2) {
System.out.println("You played Paper and I chose Rock: Paper covers Rock so you win this time!");
}
if (userOriginal == 3) {
System.out.println("You played Scissors and I chose Rock: Rock crushes Scissors so I win this time!");
}
}
//P
if (compVal == 2)
{
if (userOriginal == 1) {
System.out.println("Your played Rock and I chose Paper: Paper covers rock so I win this time!");
}
if (userOriginal == 3) {
System.out.println("You played Scissors and I chose Paper: Scissors cuts Paper so you win this time!");
}
}
//S
if (compVal == 3)
{
if (userOriginal == 1) {
System.out.println("You played Rock and I chose Sciccors: Rock crushes Scissors so you win this time");
}
if (userOriginal == 2) {
System.out.println("You played Paper and I chose Scissors: Paper is cut by Scissors so I win this time!");
}
}
}
Also, remember to always close all the resources that you used:
keyboard.close();