这个非常简单的猜谜游戏不起作用。

huangapple go评论86阅读模式
英文:

This very simple guess game does not work

问题

public static void main(String args[]) {

  1. Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);
  2. int num = 8, guess;
  3. boolean positiveguess = true;
  4. while (positiveguess) {
  5. System.out.println("Enter your guess = ");
  6. guess = reader.nextInt();
  7. if (guess != num)
  8. switch (guess) {
  9. case '1':
  10. if (num - 5 < guess && num + 5 > guess)
  11. System.out.println("Your guess is almost close! \nTry again ");
  12. break;
  13. case '2':
  14. if (num - 10 < guess && num + 10 > guess)
  15. System.out.println("You need to guess again ");
  16. break;
  17. }
  18. else
  19. positiveguess = false;
  20. }
  21. 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

  1. public static void main (String args[]){
  2. Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);
  3. int num = 8 , guess;
  4. boolean positiveguess = true;
  5. while (positiveguess) {
  6. System.out.println(&quot;Enter your guess = &quot;);
  7. guess = reader.nextInt();
  8. if (guess != num)
  9. switch(guess)
  10. {
  11. case &#39;1&#39;:
  12. if (num-5 &lt; guess &amp;&amp; num + 5 &gt;guess)
  13. System.out.println(&quot;Your guess is almost close! \nTry again &quot;);
  14. break;
  15. case &#39;2&#39;:
  16. if (num-10 &lt; guess &amp;&amp; num + 10 &gt;guess)
  17. System.out.println(&quot;You need to guess again &quot;);
  18. break;
  19. }
  20. else
  21. positiveguess = false;
  22. }
  23. System.out.println(&quot;Great !&quot;);

答案1

得分: 1

你的开关是基于用户输入而不是给定值和期望值之间的差异。首先,我建议你保持简单,使用类似于 if/else 结构的方式:

  1. public class Main {
  2. public static void main(String args[]) {
  3. Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);
  4. int num = 8, guess;
  5. boolean positiveguess = true;
  6. while (positiveguess) {
  7. System.out.println("Enter your guess = ");
  8. guess = reader.nextInt();
  9. if (guess != num) {
  10. if (num - 5 < guess && num + 5 > guess) {
  11. System.out.println("Your guess is almost close! \nTry again ");
  12. } else if (num - 10 < guess && num + 10 > guess) {
  13. System.out.println("You need to guess again ");
  14. }
  15. } else {
  16. positiveguess = false;
  17. }
  18. }
  19. System.out.println("Great !");
  20. }
  21. }

然后我建议你移除 positiveguess 变量,并将条件放入 while 循环中。同时使用 Math.abs() 来获取猜测与期望值之间的差异:

  1. public class Main {
  2. public static void main(String args[]) {
  3. Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);
  4. int num = 8;
  5. int guess = Integer.MIN_VALUE;
  6. while (guess != num) {
  7. System.out.println("Enter your guess = ");
  8. guess = reader.nextInt();
  9. int diff = Math.abs(num - guess);
  10. if (diff != 0) {
  11. if (diff < 5) {
  12. System.out.println("Your guess is almost close! \nTry again ");
  13. } else if (diff < 10) {
  14. System.out.println("You need to guess again ");
  15. } else {
  16. System.out.println("You're way too far bro");
  17. }
  18. }
  19. }
  20. System.out.println("Great !");
  21. }
  22. }
英文:

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

  1. public class Main {
  2. public static void main (String args[]) {
  3. Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);
  4. int num = 8, guess;
  5. boolean positiveguess = true;
  6. while (positiveguess) {
  7. System.out.println(&quot;Enter your guess = &quot;);
  8. guess = reader.nextInt();
  9. if (guess != num) {
  10. if (num - 5 &lt; guess &amp;&amp; num + 5 &gt; guess) {
  11. System.out.println(&quot;Your guess is almost close! \nTry again &quot;);
  12. } else if (num - 10 &lt; guess &amp;&amp; num + 10 &gt; guess) {
  13. System.out.println(&quot;You need to guess again &quot;);
  14. }
  15. } else {
  16. positiveguess = false;
  17. }
  18. }
  19. System.out.println(&quot;Great !&quot;);
  20. }
  21. }

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.

  1. public class Main {
  2. public static void main (String args[]) {
  3. Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);
  4. int num = 8;
  5. int guess = Integer.MIN_VALUE;
  6. while (guess != num) {
  7. System.out.println(&quot;Enter your guess = &quot;);
  8. guess = reader.nextInt();
  9. int diff = Math.abs(num - guess);
  10. if (diff != 0) {
  11. if (diff &lt; 5) {
  12. System.out.println(&quot;Your guess is almost close! \nTry again &quot;);
  13. } else if (diff &lt; 10) {
  14. System.out.println(&quot;You need to guess again &quot;);
  15. } else {
  16. System.out.println(&quot;You&#39;re way too far bro&quot;);
  17. }
  18. }
  19. }
  20. System.out.println(&quot;Great !&quot;);
  21. }
  22. }

答案2

得分: 0

你在这里错误地使用了 switch。看起来你想要的是类似这样的写法:

  1. switch (guess) {
  2. // 第一个情况
  3. case (num-5 < guess && num + 5 > guess):
  4. System.out.println("你的猜测几乎接近了! \n再试一次 ");
  5. // 第二个情况
  6. case (num-10 < guess && num + 10 > guess):
  7. System.out.println("你需要重新猜测 ");
  8. }

但是这是无效的语法。在Java中,switch/case 只能将元素与相同类型的其他元素(或者在更新的版本中,多个元素或类)进行比较,而不能像 if 语句那样执行复杂的条件检查。

可能你之后尝试通过结合 case '1'if 语句来“修复”它,尽管合法,但是 case '1' 只会在 guess'1'(即49)时成立,然后才会继续检查 if 的条件。

相反地,直接去掉 switch/case,直接使用 if 语句(别忘了加上 else):

  1. if (num-5 < guess && num+5 > guess) {
  2. System.out.println("你的猜测几乎接近了! \n再试一次 ");
  3. } else if (num-10 < guess && num+10 > guess) {
  4. System.out.println("你需要重新猜测 ");
  5. }

此外,你可以通过改变顺序为 (num-5 < guess && guess < num+5) 或使用 Math.abs 来找到绝对差值,并在条件中使用它,从而使这些检查更加可读。

英文:

You are using switch wrongly here. It seems you are looking for something like this:

  1. switch (guess) {
  2. // first case
  3. case (num-5 &lt; guess &amp;&amp; num + 5 &gt;guess):
  4. System.out.println(&quot;Your guess is almost close! \nTry again &quot;);
  5. // second case
  6. case (num-10 &lt; guess &amp;&amp; num + 10 &gt;guess):
  7. System.out.println(&quot;You need to guess again &quot;);
  8. }

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 &#39;1&#39; and the if statements, but while legal, case &#39;1&#39; will only be the case if guess is &#39;1&#39; (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):

  1. if (num-5 &lt; guess &amp;&amp; num+5 &gt; guess) {
  2. System.out.println(&quot;Your guess is almost close! \nTry again &quot;);
  3. } else if (num-10 &lt; guess &amp;&amp; num+10 &gt; guess) {
  4. System.out.println(&quot;You need to guess again &quot;);
  5. }

Also, you could make those checks more readable by changing the order to (num-5 &lt; guess &amp;&amp; guess &lt; num+5) or using Math.abs to find the absolute difference and use that in the condition.

huangapple
  • 本文由 发表于 2020年9月17日 22:35:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63940389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定