条件语句睡眠

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

Conditional statement sleep

问题

为什么我得到的是"缺乏"而不是"正常"?

给定三个数字:A、B 和 H。
根据医生的报告:

  • 每天至少应该睡 A 小时,
  • 但不能超过 B 小时。
  • H 是安娜睡觉的小时数。

任务:

  • 如果安娜睡眠时间少于 A 小时,则打印"缺乏"。
  • 如果她睡眠时间超过 B 小时,则打印"过量"。
  • 如果她的睡眠时间符合建议范围,则打印"正常"。
public class Main {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        int units = input.nextInt();

        int a = input.nextInt();
        int b = input.nextInt();
        int h = b - a;

        if (h < a ){
            System.out.println("缺乏");
        } else if (h > b) {
            System.out.println("过量");
        } else if (h == 8){
            System.out.println("正常");
        }
    }
}
英文:

Why do I get "Deficiency" instead of "Normal"?

Given three numbers: A, B and H.
According to Doctor report:

  • one should sleep at least A hours per day,
  • but no more than B hours.
  • H is how many hours Anna sleeps.

Task:

  • If Anna sleeps less then A hours, print "Deficiency".
  • If she sleeps more than BB hours, print "Excess".
  • If her sleep fits the recommendations, print "Normal".
public class Main {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        int units = input.nextInt();

     
        int a = input.nextInt();
        int b = input.nextInt();
        int h = b - a;

        if (h &lt; a ){
            System.out.println(&quot;Deficiency&quot;);
        } else if (h &gt; b) {
            System.out.println(&quot;Excess&quot;);
        } else if (h == 8){
            System.out.println(&quot;Normal&quot;);
        }

    }
}

答案1

得分: 5

为什么不简单地这样做呢?

  • 首先,检查她是否睡得太少。
  • 然后,如果她没有睡得太少,确保她没有睡得太多。
  • 然后通过排除法,不需要进行任何测试,所以她睡得应该是正常的。
Scanner input = new Scanner(System.in);

int tooLittle = input.nextInt();
int tooMuch = input.nextInt();
int hoursSlept = input.nextInt();

if (hoursSlept <= tooLittle ){
    System.out.println("不足");
} else if (hoursSlept >= tooMuch) {
    System.out.println("过量");
} else {
    System.out.println("正常");
}

注意,tooMuchtooLittle 之间的差异与 Anna 睡眠的时间无关。因此,您还需要提示输入这个值。

英文:

Why don't you just simplify it like this?

  • First, check that she didn't sleep too little.
  • Then if she didn't, ensure she didn't sleep too much.
  • Then by exclusion, no test is required so she must have slept normally.
Scanner input = new Scanner(System.in);

int tooLittle = input.nextInt();
int tooMuch = input.nextInt();
int hoursSlept = input.nextInt();

if (hoursSlept &lt;= tooLittle ){
    System.out.println(&quot;Deficiency&quot;);
} else if (hoursSlept &gt;= tooMuch) {
    System.out.println(&quot;Excess&quot;);
} else {
    System.out.println(&quot;Normal&quot;);
}

Notice that the difference between what is tooMuch and tooLittle has nothing to do with how much Anna slept. So you also need to prompt for that value.

答案2

得分: 3

你提到了“H是Ann睡眠的小时数”,但我从你的代码中看到你在这行计算H

int h = b - a;

在这种情况下,h始终小于a,因此触发第一个if语句:

if (h < a ){
    System.out.println("不足");
}

为了解决这个问题,也要获取h的输入,然后查看它是否在正常范围内。这应该可以工作:

public class Main {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        int units = input.nextInt();

     
        int a = input.nextInt();
        int b = input.nextInt();
        int h = input.nextInt(); // 获取Ann的睡眠时间

        if (h < a ){
            System.out.println("不足");
        } else if (h > b) {
            System.out.println("过量");
        } else if (h >= a && h <= b) // h必须在a和b的范围之间才能被视为正常
        {
            System.out.println("正常");
        }

    }
}
英文:

You mentioned that "H is how many hours Ann sleeps." but I see from your code that you're calculating H at this line:

int h = b - a;

in which case h will ALWAYS be smaller than a hence triggering the first if statement:

if (h &lt; a ){
    System.out.println(&quot;Deficiency&quot;);
}

To fix this, get an input for h as well and then see if it's in normal range. This should work:

public class Main {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        int units = input.nextInt();

     
        int a = input.nextInt();
        int b = input.nextInt();
        int h = input.nextInt(); // get an input for Ann&#39;s sleep time

        if (h &lt; a ){
            System.out.println(&quot;Deficiency&quot;);
        } else if (h &gt; b) {
            System.out.println(&quot;Excess&quot;);
        } else if (h &gt;= a &amp;&amp; h &lt;= b) // h must fall between a and b ranges to be considered normal
        {
            System.out.println(&quot;Normal&quot;);
        }

    }
}

答案3

得分: 0

这与要求不太匹配。您应该为 h 获取输入 - 您不能从 ab 计算它,因为它们对于它们应该获得的内容是常量;它们与它们实际获得的内容无关。

如果 a < 2b,您总会得到不足。否则,您总会得到“过量”(特殊情况是 b - a = 8,在这种情况下您将得到“正常”;还有一种情况是 a = b,在这种情况下您将不会打印任何内容)。

第三种情况(将 h 固定为 8)不符合要求,也不完全正确。要求不仅没有说 8 小时的睡眠是正常的,事实上这也并不准确 - 8 小时的睡眠只是一个平均值 - 有些人可能需要更多或更少。要求是要检查 h 是否在 ab 之间,而不是要检查它是否恰好为 8 小时。

此外,从用户输入的角度看,您应该提示用户输入,如下所示:

System.out.println("输入最少睡眠时间:");
int a = input.nextInt();

System.out.println("输入最多睡眠时间:");
int b = input.nextInt();

System.out.println("输入实际睡眠时间:");
int h = input.nextInt();

最后,您实际上从未使用过 unit,因此这要么是不必要的,要么是您忘记实现的要求。

英文:

This doesn't really match what the requirements say. You should get an input for h - you can't compute it from a or b because they're constants for what they're supposed to be getting; they have nothing to do with what they actually get.

If a &lt; 2b, you'll always get a deficiency. Otherwise, you'll always get "excess" (except in the special case where b - a = 8, in which case you'll get "normal"; also, in the case where a = b, you won't print anything at all).

The third case (hardcoding it to h == 8) doesn't meet the requirements and isn't really true. Not only does the requirements not say that 8 hours of sleep is normal, it's not even true - 8 hours of sleep is only an average - some people might need somewhat more or somewhat less than that. The requirements say to check to see if h is between a and b, not to check to see if it's exactly 8 hours.

Also, from a user input perspective, you should prompt the user for input, like this:

System.out.println(&quot;Enter the minimum amount of sleep:&quot;);
int a = input.nextInt();

System.out.println(&quot;Enter the maximum amount of sleep:&quot;);
int b = input.nextInt();

System.out.println(&quot;Enter the actual amount of sleep:&quot;);
int h = input.nextInt();

Finally, you never actually use unit, so this is either unnecessary or there's a requirement you forgot to implement.

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

发表评论

匿名网友

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

确定