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

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

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(&quot;Enter your guess = &quot;);
guess = reader.nextInt();
if (guess != num)
    switch(guess)
    {
        case &#39;1&#39;:
            if (num-5 &lt; guess &amp;&amp; num + 5 &gt;guess)
                System.out.println(&quot;Your guess is almost close! \nTry again &quot;);
       break;
        case &#39;2&#39;:
            if (num-10 &lt; guess &amp;&amp; num + 10 &gt;guess)
                System.out.println(&quot;You need to guess again &quot;);
        break;    
    }              
else
    positiveguess = false;
    
}
System.out.println(&quot;Great !&quot;);

答案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(&quot;Enter your guess = &quot;);
            guess = reader.nextInt();
            if (guess != num) {
                if (num - 5 &lt; guess &amp;&amp; num + 5 &gt; guess) {
                    System.out.println(&quot;Your guess is almost close! \nTry again &quot;);
                } else if (num - 10 &lt; guess &amp;&amp; num + 10 &gt; guess) {
                    System.out.println(&quot;You need to guess again &quot;);
                }
            } else {
                positiveguess = false;
            }
        }
        System.out.println(&quot;Great !&quot;);
    }
}

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(&quot;Enter your guess = &quot;);
            guess = reader.nextInt();

            int diff = Math.abs(num - guess);
            if (diff != 0) {
                if (diff &lt; 5) {
                    System.out.println(&quot;Your guess is almost close! \nTry again &quot;);
                } else if (diff &lt; 10) {
                    System.out.println(&quot;You need to guess again &quot;);
                } else {
                    System.out.println(&quot;You&#39;re way too far bro&quot;);
                }
            }
        }
        System.out.println(&quot;Great !&quot;);
    }
}

答案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 &lt; guess &amp;&amp; num + 5 &gt;guess):
        System.out.println(&quot;Your guess is almost close! \nTry again &quot;);
    // second case
    case (num-10 &lt; guess &amp;&amp; num + 10 &gt;guess):
        System.out.println(&quot;You need to guess again &quot;);
}

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):

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

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:

确定