Here is what I am trying to accomplish with Java:
A simple guessing game asks the player to guess a number between 1 and 50. Hints are returned depending on how far from the number the guess is. Add other methods for playing the game omitted--you need not supply any of them.

20 or more above the number: The hint is "Burning Hot"
10 to 19 above the number: The hint is "Hot"
5 to 9 above the number: The hint is "Warm"
4 below the number to 4 above: The hint is "Pleasant"
5 to 9 below the number: The hint is "Cool"
10 to 19 below the number: The hint is "Cold"
20 or more below the number: The hint is "Ice Cold"

Here is my code so far, but I am having a hard time from this point:

``````import java.util.Random;
/**
A class to store a number to guess and return a closeness hint
*/
public class GuessNumber
{
private int number;

/**
Construct an GuessNumber with a random value between 1 and 50
*/
public GuessNumber()
{
Random r = new Random();
number = r.nextInt(50) + 1;
}

/**
Determine how close the guess is and return a hint
@return a string describing, in terms of hotness or coldness, how
close the guess is to the number
*/
public String howClose(int guess)
{
//I need code here but cannot quite get it

}

// This method is used to check your work

public static String check(int number, int guess)
{
GuessNumber gn = new GuessNumber();
gn.number = number;
return gn.howClose(guess);
}
}
``````

## All 2 Replies

what is "from this point" ? What is it exactly you are trying to implement now, and what trouble do you have with it?

The logic seems a bit weird, shouldn't it be Burning hot when the guess is within 4 numbers? Anyhow, the code (to your description):

``````public String howClose(int guess)
{
if ( guess - number >= 20 )
{
return "Burning Hot";
}
else if ( guess - number >= 10 && guess - number <= 19 )
{
return "Hot";
}
else if ( guess - number >= 5 && guess - number <= 9 )
{
return "Warm";
}
else if ( guess - number >= -4 && guess - number <= 4 )
{
return "Pleasant";
}
else if ( guess - number >= -9 && guess - number <= -5 )
{
return "Cool";
}
else if ( guess - number >= -19 && guess - number <= -10 )
{
return "Cold";
}
else if ( guess - number <= -20 )
{
return "Ice cold";
}
return null;
}
``````
Be a part of the DaniWeb community

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