vendredi 26 août 2016

JAVA: If condition is met, yet it acts as though it isn't [duplicate]

This question already has an answer here:

I'm writing a java "guess my number" sort of game. In the first part of the game, the player selects the difficulty of the game (Easy, Medium, Hard). My problem is that when I play, the game will return "Invalid Response" regardless of what I type into the text box. I know what I'm typing in matches a valid response, but it doesn't seem to think so. What am I doing wrong?

I've attached the first part of the game code below, where the player selects the difficulty. This is process then eventually determines the variable "guessThis0," which is the number to guess during the actual game...

//Guess My Number Game

import java.util.Scanner;
import javax.swing.JOptionPane;

public class playGuessMyNumberGame {

@SuppressWarnings({ "unused", "resource" })
public static void main(String[] args)
{

    boolean gameOver = false;  //becomes 'true' when player WINS or LOSES
    boolean playAgain = true;  //decides if player would like to play again
    boolean setUpDone = false; //for player setup loop
    int guessThis0;  //Target number
    int guessesRemaining = 12;  //Player starting guesses
    int highLimit = 500;  //Highest the Answer can be. Set by (player-chosen) difficulty

    //PLAYER SETUP
    while(setUpDone == false)
    {
        /*
        Scanner playerDifficulty0 = new Scanner(System.in);
        String playerDifficulty = playerDifficulty0.next();
        */
        String playerDifficulty = JOptionPane.showInputDialog("CHOOSE DIFFICULTY \nPlease Enter: 'Easy,' 'Medium,' or 'Hard.' ");
        if(playerDifficulty == "Easy")
        {
            highLimit = 250;
            setUpDone = true;
        }
        if(playerDifficulty == "Hard")
        {
            highLimit = 1500;
            setUpDone = true;
        }
        if(playerDifficulty == "Moderate")
        {
            highLimit = 750;
            setUpDone = true;
        }
        else //This is what it returns regardless of what I type in:
        {
            System.out.println("Not a valid response. Please try again. \n");
        } // ^ This is what it return regardless of what I type in ^
    }
System.exit(0);
guessThis0 = 0 + (int)(Math.random() * highLimit);

//PLAY GAME
}
}

Aucun commentaire:

Enregistrer un commentaire