简体   繁体   English

我的逻辑有什么问题(Java语法)

[英]What's wrong with my logic (Java syntax)

I'm trying to make a simple program that picks a random number and takes input from the user. 我正在尝试制作一个简单的程序,该程序可以选择一个随机数并从用户那里获取输入。 The program should tell the user if the guess was hot (-/+ 5 units) or cold, but I never reach the else condition. 该程序应该告诉用户猜测是热的(-/ + 5个单位)还是冷的,但是我从未达到过else条件。

Here's the section of code: 这是代码部分:

    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..");
    }
}
int min = guess - 5;
int max = guess + 5;

Should be: 应该:

int min = actualAnswer - 5;
int max = actualAnswer + 5;

Here is your problem: 这是您的问题:

int min = guess - 5;
int max = guess + 5;

min is ALWAYS lesser than guess and max is ALWAYS greater than guess . min总是小于guessmax总是大于guess

You need to pass in the actual answer, not just the guess, since the logic can't tell what the appropriate mix/max should be otherwise: 您需要传递实际答案,而不仅仅是猜测,因为逻辑无法告诉您适当的混合/最大值应该是什么:

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..");
    }
}

You are defining guess as being both > min (because int min = guess - 1 ) and < max (because int max = guess + 5 ). 您将guess定义为> min (因为int min = guess - 1 )和< max (因为int max = guess + 5 )。 So of course the first condition is always met. 因此,当然始终满足第一个条件。 You should be using the actual secret value when defining min and max . 定义minmax时,应该使用实际的秘密值。

min and max should use the value the player is looking for (secret value) rather than the value the player provided. minmax应使用玩家正在寻找的值(秘密值),而不是玩家提供的值。 As it is, min and max change every time the player gives a guess and you're not even using the secret value. 实际上,每次玩家进行猜测时, minmax改变,甚至您都不使用秘密值。

您是根据猜测来计算最小值和最大值,因此猜测始终在最小值和最大值之间,因此(guess> min)&&(guess <max)始终为true。

Lets use an example: 让我们举个例子:

int guess = 20;
int min = guess - 5 = 15;
int max = guess + 5 = 25;

therefore 因此

min < guess < max

Why? 为什么?

Because you're comparing guess to its self! 因为您正在将猜测与其自身进行比较! I think you need to have the actual answer, not the guess 我认为您需要的是实际答案,而不是猜测

I can't put the full thing in a comment, so here's what your code 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..");
    }

Another solution : 另一个解决方案:

    public static void giveHint (int actual, int guess) {
         if(Math.abs(actual - guess) <= 5) {
             System.out.println("Hot");
             return;
         }

         System.out.println("Cold");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM