条件语句睡眠

Kal*_*ali 1 java

为什么我得到“缺陷”而不是“正常”?

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

  • 每天至少要睡一个小时,
  • 但不超过B小时。
  • H 是安娜睡了多少小时。

任务:

  • 如果 Anna 的睡眠时间少于 A 小时,则打印“Deficiency”。
  • 如果她的睡眠时间超过 BB 小时,则打印“Excess”。
  • 如果她的睡眠符合建议,请打印“正常”。
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("Deficiency");
        } else if (h > b) {
            System.out.println("Excess");
        } else if (h == 8){
            System.out.println("Normal");
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

WJS*_*WJS 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("Deficiency");
} else if (hoursSlept >= tooMuch) {
    System.out.println("Excess");
} else {
    System.out.println("Normal");
}
Run Code Online (Sandbox Code Playgroud)

请注意,之间有什么不同tooMuch,并tooLittle没有任何与安娜多少睡觉。因此,您还需要提示输入该值。