The program is supposed to ask the user for the range of the guesing game (1-10), ask them how many guesses they want, and then ask them for a guess. Then it prints out Cold if he guess is greater than 25% of the answer, print WARM if its between 10%-25% and print HOT if its within 10%...PLEASE HELP!

/**
 * 
 */
package edu.ilstu;

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

/**
 * @author Scott Swingle
 *
 */
public class GuessingGame {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner keyboard = new Scanner (System.in);
        Random generator = new Random();

        int guesses;
        int guessNum = 0;
        double difference = 0;
        int answer;
        double guess = 0;
        String another = "y";
        boolean flag = false;
        boolean anotherFlag = true;
        int count = 1;

        System.out.print("Please enter a number to indicate the range of the game (10,20,30)");
        guessNum = keyboard.nextInt();
        System.out.println("How many guesses should I allow?");
        guesses = keyboard.nextInt();
        System.out.println("Let's Play. I've chosen my number.");

        answer = generator.nextInt(guessNum) + 1;
        System.out.println("It's a whole number between 1 and " + guessNum + (": "));
        flag = false;
        guess = keyboard.nextInt();

        while(!flag || count <= guesses)
        {
            answer = generator.nextInt(guessNum) + 1;
            System.out.println("It's a whole number between 1 and " + guessNum + (": "));
            flag = false;
            guess = keyboard.nextInt();

            {
                if(guess == answer) 
                {
                    System.out.println("You guessed correctly! Good Job");
                    flag = true;
                }   
                else if(difference / guessNum <= .10)
                {
                    System.out.println("Hot! Try Again: ");
                    count++;
                }
                else if(difference /guessNum <= .25)
                {
                    System.out.println("Warm! Try Again: ");
                    count++;
                }
                else
                {
                    System.out.println("Cold! Try Again ");
                    count++;
                }


                if(guess > answer)
                {
                    difference = guess - answer;        
                }
                else if (guess < answer)
                {
                    difference = answer - guess;

                guess = keyboard.nextInt();
            }
            System.out.println();
            System.out.println("Would you like to play again? (y/n)");
            another = keyboard.next();
            if(another.equalsIgnoreCase("y") == true)
            {
                anotherFlag = true;
            }
            else 
            {
                anotherFlag = false;
            }
    }



    }

    }
}

Recommended Answers

All 2 Replies

what is it you are having trouble with?
it's easier for us to look into your question if we know what the actual question is.

but try and remove redundant code. an example I gave a few days earlier as well:

       if(another.equalsIgnoreCase("y") == true)
        {
            anotherFlag = true;
        }
        else 
        {
            anotherFlag = false;
        }

can be rewritten as:

       if(another.equalsIgnoreCase("y"))
        {
            anotherFlag = true;
        }
        else 
        {
            anotherFlag = false;
        }

since the equals method returns a boolean already, but it would be a lot easier if you just wrote:

anotherFlag = another.equalsIgnoreCase("y");

it reduces the lines of code, makes it a lot easier to read, and saves us all some time.

... and even better, try using the Java conventions for naming variables, especially booleans. What do you think someone will understand from flag = false;? What does that mean? How does that relate toanotherFlag = false;?
Boolean names ideally describe the thing that will be true if the variable is true, and don't be afraid of long names if that's what it takes to make them clear, eg

boolean userWantsAnotherGame = another.equalsIgnoreCase("y");

this makes the code incredibly clear and easy to read, for example when it leads to lines like:

while (userWantsAnotherGame) ...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.