Reputation: 17
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");
}
}
}
Upvotes: 0
Views: 8823
Reputation: 533492
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
Upvotes: 3