Noob1029
Noob1029

Reputation: 39

Java Why is my While loop being ignored for every input I type in?

I'm trying to figure out why my while loop in my main method is being ignored even though I haven't entered "quit". The program is supposed to take in a user's input (converted to int), have the computer generate a random int(rock, paper, or scissors), compare the two ints, print out the answer, and do all over again until "quit" or any other unrecognized input(not rock, paper, or scissors) is entered.

For example: UserInput = rock; Output: Goodbye!!! (while loop is just skipped, goes straight to if loop even though "userInput"(converted to int 1) doesn't equal "quit"(converted to int 0). Trying to fix it so that if UserInput = "rock"(converted to 1), the computer randomly generates "paper"(converted to 2), and the output will be that "paper" wins.

I've been trying to figure out this for hours, so any help is appreciated, thanks.

package rockPaperScissors;
import java.util.*

public class RockPaperScissors {
    public static final int quit = 0;
    public static final int rock = 1;
    public static final int paper = 2;
    public static final int scissors = 3;
    static Scanner console = new Scanner(System.in);

public static void main(String [] args) {
    //gets userHand from user as int
    int userHand = promptUserForHand();
    while(userHand != quit) {
        //gets computerHand from the computer(random) as int
        int computerHand = generateRandomHand();
        //compares userHand to computerHand, determines the winner
        String winner = determineWinner(userHand, computerHand);
        //prints out the winner
        System.out.println("The winner is the: " + winner);
        //starts the next round
        userHand = promptUserForHand();
    }
    //if userHand equals quit, stop program and say goodbye
    if(userHand == quit) {
        System.out.println("Goodbye!!!");
        console.close();        
    }
}

public static int promptUserForHand() {
    //gets userInput from user
    System.out.println("Please input either rock, paper, scissors (or enter quit to stop)");
    String userInput = console.next(); 
    //converts String into int, so userHand and computerHand can be compared
    int userHand = 0;
    if(userInput == "rock" || userInput == "Rock" || userInput == "r") {
        userHand  = rock;
    }
    else if(userInput == "paper" || userInput == "Paper" || userInput == "p") {
        userHand = paper;
    }
    else if(userInput == "scissors" || userInput == "Scissors" || userInput == "s"){
        userHand = scissors;
    }
    else {
        userHand = quit;
    }
    return userHand;
}

//generates computerHand from computer(randomly chooses either rock(1), paper(2), or scissors(3))
public static int generateRandomHand() {
    Random r = new Random();
    int computerHand = r.nextInt(4) + 1;
    return computerHand;
}

//compares userHand to computerHand to determine the winner
public static String determineWinner(int userHand, int computerHand) {
    String winner = " ";
    if(userHand == 1 && computerHand == 2) {
        winner = "Computer!!!";
    }
    else if(userHand == 1 && computerHand == 3) {
        winner = "User!!!";
    }
    else if(userHand == 2 && computerHand == 1) {
        winner = "User!!!";
    }
    else if(userHand == 2 && computerHand == 3) {
        winner = "Computer!!!";
    }
    else if(userHand == 3 && computerHand == 1) {
        winner = "Computer!!!";
    }
    else if(userHand == 3 && computerHand == 2) {
        winner = "User!!!";
    }
    else {
        winner = "Tie!!!";
    }       
    return winner;
}

}

Upvotes: 1

Views: 196

Answers (1)

MD Ruhul Amin
MD Ruhul Amin

Reputation: 4502

You should not compare string using == in java. instead use: equals() or equalsIgnoreCase() method.

for example:

instead of:

 if(userInput == "rock" || userInput == "Rock" || userInput == "r") 

use:

if(userInput.equalsIgnoreCase("rock") || userInput.equalsIgnoreCase("r"))

Why: Check this answers: Why would you use String.Equals over ==?

Upvotes: 0

Related Questions