Connor
Connor

Reputation: 1

int variable remains at 0, while loop not running properly as a result

What I really am stuck with is the second to last variable userGuessDifference. It remains at zero making my second while loop not run properly as it just keeps going back to the first else if statement.

public class GuessingGame {

/**
 * @param args
 */
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
Random generator = new Random();
int difficulty = 0;
int guesses = 0;
int userGuess = 0;
int correctAnswer = 0;
int counter = 0;
int userGuessDifference = (Math.abs(correctAnswer) - Math.abs(userGuess));
boolean flag = false;

System.out.println("We are going to play a number guessing game."); 
System.out.println(" ");

System.out.println("Choose your difficulty:");
System.out.println("Pick a number - 10 is easy, 25 is medium, 50 is hard.");
difficulty = keyboard.nextInt();

if (difficulty == 10)
{
    guesses = 3;
    System.out.println("You have 3 guesses, make them count!");
}   
else if (difficulty == 25)
{   
    guesses = 5;
    System.out.println("You have 5 guesses, make them count!");
}
else if (difficulty == 50) 
{
    guesses = 6;
    System.out.println("You have 6 guesses, make them count!");
}
else
{   
    System.out.println("If you can't follow instructions, I'm going to make this very difficult for you!");
    difficulty = (difficulty * 100);
    guesses = 1;
}

System.out.println(" ");
System.out.println("Ok, I have my number. Time to play.");
correctAnswer = generator.nextInt(difficulty) + 1;
System.out.println("Pick a whole number between 1 and " + difficulty + ":");
userGuess = keyboard.nextInt();

while (!flag || (counter <= guesses))
{

    if (userGuess == correctAnswer)
    {
        System.out.println("CONGRATS YOU WIN!");
        flag = true;
    }

    else if ((userGuessDifference <= (difficulty * .10)))
    {
        System.out.println("HOT!");
        userGuess = keyboard.nextInt();
        counter++;
    }

    else if ((userGuessDifference < (difficulty * .25)) && (userGuessDifference > (difficulty * .10)))
    {
        System.out.println("Warm...");
        userGuess = keyboard.nextInt();
        counter++;
    }

    else
    {
        System.out.println("Ice cold.");
        userGuess = keyboard.nextInt();
        counter++;
    }

}
}
}

Upvotes: 0

Views: 151

Answers (1)

Paolo
Paolo

Reputation: 1691

As @SotiriosDelimanolis wrote, you never reassign userGuessDifference. This should be done inside the while loop.

Moreover, there is another problem with your code: if you guess the number, the program just prints "CONGRATS YOU WIN!" forever, but it seems to me that you wanted to quit from the while loop once the user guesses the number (I guess the flag variable was introduced for this reason).

I slightly changed your code in order to meet this requirement:

import java.util.Random;
import java.util.Scanner;

public class GuessingGame {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        Random generator = new Random();
        int difficulty = 0;
        int guesses = 0;
        int userGuess = 0;
        int correctAnswer = 0;
        int counter = 0;
        System.out.println("We are going to play a number guessing game.");
        System.out.println(" ");
        System.out.println("Choose your difficulty:");
        System.out.println("Pick a number - 10 is easy, 25 is medium, 50 is hard.");
        difficulty = keyboard.nextInt();
        if (difficulty == 10) {
            guesses = 3;
            System.out.println("You have 3 guesses, make them count!");
        } else if (difficulty == 25) {
            guesses = 5;
            System.out.println("You have 5 guesses, make them count!");
        } else if (difficulty == 50) {
            guesses = 6;
            System.out.println("You have 6 guesses, make them count!");
        } else {
            System.out.println("If you can't follow instructions, I'm going to make this very difficult for you!");
            difficulty = (difficulty * 100);
            guesses = 1;
        }
        System.out.println(" ");
        System.out.println("Ok, I have my number. Time to play.");
        correctAnswer = generator.nextInt(difficulty) + 1;
        System.out.println("Pick a whole number between 1 and " + difficulty + ":");
        userGuess = keyboard.nextInt();
        while (counter <= guesses) {
            // int userGuessDifference = (Math.abs(correctAnswer) - Math
            //      .abs(userGuess));
                    int userGuessDifference = Math.abs(correctAnswer - userGuess);
            if (userGuess == correctAnswer) {
                System.out.println("CONGRATS YOU WIN!");
                break;
            }
            else if ((userGuessDifference <= (difficulty * .10))) {
                System.out.println("HOT!");
            }
            else if ((userGuessDifference < (difficulty * .25))
                    && (userGuessDifference > (difficulty * .10))) {
                System.out.println("Warm...");
            }
            else {
                System.out.println("Ice cold.");
            }
            userGuess = keyboard.nextInt();
            counter++;
        }
    }
}

Upvotes: 1

Related Questions