I am a beginning Java programmer and am working on a program that plays Rock Paper Scissors with the user.
To find out whether the user wants rock, paper, or scissors, I ask they in a JOptionPane. My problem is that their answer is case-sensitive. I put in a toUpperCase() object but the program still gives me an error when I type a lowercase answer. What am I doing wrong? Thank you
public class RockPaperScissors {
final static int ROCK = 0, SCISSORS = 1, PAPER = 2;
public static void main(String args[]) {
int z = 0;
String user_choice, ComputerRockPaperScissors, play_again;
Random m = new Random();
do {
user_choice = JOptionPane.showInputDialog("Rock, Paper, Scissors?");
user_choice.toUpperCase();
int comp_choice = m.nextInt(3);
ComputerRockPaperScissors = convert(comp_choice);
JOptionPane.showMessageDialog(null, "You chose " + user_choice + " and the computer chose " + ComputerRockPaperScissors + ". " + whoWon(user_choice, comp_choice));
play_again = JOptionPane.showInputDialog("Would you like to play again?");
play_again.toUpperCase();
} while (play_again.equals("Yes"));
}
public static String whoWon(String user_input, int comp_choice) {
int userRockPaperScissors = 9;
String end = "ERROR";
//convert String response to integer
if (user_input.equals("Rock")) {
userRockPaperScissors = ROCK;
}
if (user_input.equals("Scissors")) {
userRockPaperScissors = SCISSORS;
}
if (user_input.equals("Paper")) {
userRockPaperScissors = PAPER;
}
//get result
if (((userRockPaperScissors == PAPER) && (comp_choice == SCISSORS))
|| ((userRockPaperScissors == ROCK) && (comp_choice == PAPER))
|| ((userRockPaperScissors == SCISSORS) && (comp_choice == ROCK))) {
end = "You Lose";
}
if (((userRockPaperScissors == SCISSORS) && (comp_choice == PAPER))
|| ((userRockPaperScissors == PAPER) && (comp_choice == ROCK))
|| ((userRockPaperScissors == ROCK) && (comp_choice == SCISSORS))) {
end = "You Win";
}
if (userRockPaperScissors == comp_choice) {
end = "Tie!";
}
return end;
}
public static String convert(int v) {
String random_choice = "Rock";
if (v == 1) {
random_choice = "Scissors";
}
if (v == 2) {
random_choice = "Paper";
}
return random_choice;
}
}


Sign In
Create Account

Guest_Intr3p1dN4t3_*
Back to top










