I have tried plenty of times to get this to work. I want to make it so if a user inputs anything other than what is meant to be input, then it gives them an error message and prompts them to input a new answer. However, every time I attempt this, it displays an error message (my error message) no matter what I input, even if it's correct. Help?
import java.util.Random;
import java.util.Scanner;
public class RandomSelect
{
public static void main(String[] args)
{
String [] arr = {"rock", "paper", "scissors"};
Random random = new Random();
Scanner scan = new Scanner(System.in);
System.out.println("Please select: rock, paper, or scissors?");
String myChoice = scan.nextLine();
boolean myChoiceIsntCorrect = false;
if ((!myChoice.equalsIgnoreCase("rock")) || (!myChoice.equalsIgnoreCase("paper")) || (!myChoice.equalsIgnoreCase("scissors")))
{
myChoiceIsntCorrect = true;
}
while ( myChoiceIsntCorrect == true )
{
System.out.println("Your input wasn't either rock, paper, or scissors. Please select one of the 3.");
myChoice = scan.nextLine();
}
int select = random.nextInt(arr.length);
System.out.println("Computer selected " + arr[select] + ".");
if (arr[select] == "rock" && myChoice.equalsIgnoreCase("paper"))
System.out.println("You win!");
if (arr[select] == "rock" && myChoice.equalsIgnoreCase("scissors"))
System.out.println("You lose!");
if (arr[select] == "rock" && myChoice.equalsIgnoreCase(arr[select]))
System.out.println("It's a tie!");
if (arr[select] == "paper" && myChoice.equalsIgnoreCase("rock"))
System.out.println("You lose!");
if (arr[select] == "paper" && myChoice.equalsIgnoreCase("scissors"))
System.out.println("You win!");
if (arr[select] == "paper" && myChoice.equalsIgnoreCase(arr[select]))
System.out.println("It's a tie!");
if (arr[select] == "scissors" && myChoice.equalsIgnoreCase("paper"))
System.out.println("You lose!");
if (arr[select] == "scissors" && myChoice.equalsIgnoreCase("rock"))
System.out.println("You win!");
if (arr[select] == "scissors" && myChoice.equalsIgnoreCase(arr[select]))
System.out.println("It's a tie!");
}
}
I have attempted it without the boolean, and thought the boolean might work. But it didn't work with or without. What am I doing wrong? And yes, I am a newbie to java. I am taking a java course at school atm.