This question already has an answer here:
- How do I compare strings in Java? 23 answers
Can anyone tell me why this does not work? I keep getting the else statement returned. import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String userChoice;
int rock = 1, paper = 2, scissors = 3, userChoiceNum = 0;
double compChoice;
System.out.println("Rock, paper, or scissors?");
userChoice = input.nextLine();
compChoice = Math.random() * 3 + 1;
compChoice = (int)(compChoice * 10)/10;
if (userChoice == "rock" || userChoice == "Rock") {
userChoiceNum = userChoiceNum + 1;
}
else if (userChoice == "Paper" || userChoice == "paper") {
userChoiceNum = userChoiceNum + 2;
}
else if (userChoice == "Scissors" || userChoice == "scissors") {
userChoiceNum = userChoiceNum + 3;
}
else {
System.out.println("That doesn't exist in this game!");
}
}
}
I just want it so that when the user inputs rock or any other one, it assigns an int value with the corresponding number for the one the user entered. I do not have any idea why it is not working. Please help.
Aucun commentaire:
Enregistrer un commentaire