I try to make a very simple guess game and i wanted to add an exciting addition by doing an analysis of the guesswork from the user using switch function but I was surprised that it did not run that analysis
public static void main (String args[]){
Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);
int num = 8 , guess;
boolean positiveguess = true;
while (positiveguess) {
System.out.println("Enter your guess = ");
guess = reader.nextInt();
if (guess != num)
switch(guess)
{
case '1':
if (num-5 < guess && num + 5 >guess)
System.out.println("Your guess is almost close! \nTry again ");
break;
case '2':
if (num-10 < guess && num + 10 >guess)
System.out.println("You need to guess again ");
break;
}
else
positiveguess = false;
}
System.out.println("Great !");
Your switch is based on the user input and not the difference between given and exepcted value. First I suggest you to keep it simple with if/else
structure like
public class Main {
public static void main (String args[]) {
Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);
int num = 8, guess;
boolean positiveguess = true;
while (positiveguess) {
System.out.println("Enter your guess = ");
guess = reader.nextInt();
if (guess != num) {
if (num - 5 < guess && num + 5 > guess) {
System.out.println("Your guess is almost close! \nTry again ");
} else if (num - 10 < guess && num + 10 > guess) {
System.out.println("You need to guess again ");
}
} else {
positiveguess = false;
}
}
System.out.println("Great !");
}
}
Then I suggest you to remove your positiveguess
and do a while
loop with your condition inside. And use Math.abs()
to get the difference between the guess and the expected value.
public class Main {
public static void main (String args[]) {
Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);
int num = 8;
int guess = Integer.MIN_VALUE;
while (guess != num) {
System.out.println("Enter your guess = ");
guess = reader.nextInt();
int diff = Math.abs(num - guess);
if (diff != 0) {
if (diff < 5) {
System.out.println("Your guess is almost close! \nTry again ");
} else if (diff < 10) {
System.out.println("You need to guess again ");
} else {
System.out.println("You're way too far bro");
}
}
}
System.out.println("Great !");
}
}
You are using switch
wrongly here. It seems you are looking for something like this:
switch (guess) {
// first case
case (num-5 < guess && num + 5 >guess):
System.out.println("Your guess is almost close! \nTry again ");
// second case
case (num-10 < guess && num + 10 >guess):
System.out.println("You need to guess again ");
}
But this is not valid syntax . In Java, a switch/case
can only compare elements to other elements of the same type (or, in newer version, multiple elements, or a class), not perform complex conditional checks like if
can. Probably you then tried to "fix" it by combining case '1'
and the if
statements, but while legal, case '1'
will only be the case if guess
is '1'
(ie 49), and only then will continue to check the if
condition.
Instead, just drop the switch/case
and use your if
statements directly (don't forget the else
):
if (num-5 < guess && num+5 > guess) {
System.out.println("Your guess is almost close! \nTry again ");
} else if (num-10 < guess && num+10 > guess) {
System.out.println("You need to guess again ");
}
Also, you could make those checks more readable by changing the order to (num-5 < guess && guess < num+5)
or using Math.abs
to find the absolute difference and use that in the condition.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.