What's wrong with my logic (Java syntax)
I am trying to create a simple program that picks a random number and takes data from the user. The program should tell the user whether the guess was hot (- / + 5 units) or cold, but I never reach the else clause.
Here's the code section:
public static void giveHint (int guess) {
int min = guess - 5;
int max = guess + 5;
if ((guess > min) && (guess < max)) {
System.out.println("Hot..");
} else {
System.out.println("Cold..");
}
}
+2
a source to share
9 replies
You need to pass the actual answer, not just a guess, since the logic can't figure out what the corresponding mix / max should be:
public static void giveHint (int guess, int actual) {
int min = actual - 5;
int max = actual + 5;
if ((guess > min) && (guess < max)) {
System.out.println("Hot..");
} else {
System.out.println("Cold..");
}
}
0
a source to share
I can't put everything in a comment, so here's what it should be for your code:
public static void giveHint (int guess, int actual) {
int min = actual - 5;
int max = actual + 5;
if ((guess > min) && (guess < max)) {
System.out.println("Hot..");
} else {
System.out.println("Cold..");
}
-1
a source to share