英文:
This very simple guess game does not work
问题
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 !");
}
英文:
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 !");
答案1
得分: 1
你的开关是基于用户输入而不是给定值和期望值之间的差异。首先,我建议你保持简单,使用类似于 if/else
结构的方式:
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 !");
}
}
然后我建议你移除 positiveguess
变量,并将条件放入 while
循环中。同时使用 Math.abs()
来获取猜测与期望值之间的差异:
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 !");
}
}
英文:
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 !");
}
}
答案2
得分: 0
你在这里错误地使用了 switch
。看起来你想要的是类似这样的写法:
switch (guess) {
// 第一个情况
case (num-5 < guess && num + 5 > guess):
System.out.println("你的猜测几乎接近了! \n再试一次 ");
// 第二个情况
case (num-10 < guess && num + 10 > guess):
System.out.println("你需要重新猜测 ");
}
但是这是无效的语法。在Java中,switch/case
只能将元素与相同类型的其他元素(或者在更新的版本中,多个元素或类)进行比较,而不能像 if
语句那样执行复杂的条件检查。
可能你之后尝试通过结合 case '1'
和 if
语句来“修复”它,尽管合法,但是 case '1'
只会在 guess
是 '1'
(即49)时成立,然后才会继续检查 if
的条件。
相反地,直接去掉 switch/case
,直接使用 if
语句(别忘了加上 else
):
if (num-5 < guess && num+5 > guess) {
System.out.println("你的猜测几乎接近了! \n再试一次 ");
} else if (num-10 < guess && num+10 > guess) {
System.out.println("你需要重新猜测 ");
}
此外,你可以通过改变顺序为 (num-5 < guess && guess < num+5)
或使用 Math.abs
来找到绝对差值,并在条件中使用它,从而使这些检查更加可读。
英文:
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'
(i.e. 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论