【问题标题】:switch, if-else and math开关、if-else 和数学
【发布时间】:2015-03-01 06:32:48
【问题描述】:

这只是代码的一部分。因此,由于某种原因,当我尝试运行它并且值在可接受的范围内时,我得到一个“无效的重量!”响应,但不应将其作为 else 语句。

所以...我选择 1 作为我想要的选项,我应该输入以月为单位的年龄和以 kg 为单位的体重。年龄必须在 0 到 24 个月之间。重量必须在 2 到 15 公斤之间。

If weight(kg) >= (age(months))/3 + 2 and 
   weight(kg) <= (5*age(months))/12 + 5... 

如果不在该范围内,我想打印出“健康” - 不健康。

System.out.println("(1) months then weight in kg");

Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int selection = input.nextInt();
switch(selection){
case 1:
    System.out.println("Enter age in months: ");
    double month = input.nextDouble(); 
    if(month >= 0 && month <= 24){   
        System.out.println("Enter a weight in kg: ");
    } else{
        System.out.println("Not a baby anymore");
        System.exit(0);    
    }
    double weight = input.nextInt();
    if(weight >= 2 && weight <= 15 && weight >= (month/3) +2 && weight <= ((5 * month)/12) +5) {
        System.out.println("Healthy!");
    } else if(weight <= (month/3) +2 && weight >= ((5 * month)/12) +5) {
        System.out.println("Not Healthy!");
    } else{
        System.out.println("Invalid Weight!");
    }
    break;

...
}

【问题讨论】:

  • 您可能无法正确接收输入。打印出 weight 变量,看看它是否是一个真实的值而不是一些垃圾。此外,您使用的是input.nextInt(),但您将weight 设置为double。保持数据类型一致。
  • 哪一系列的输入让你失败了?
  • 数据类型对我来说是一个令人厌烦的错误。让 or 条件解决了我的问题。谢谢

标签: java math


【解决方案1】:

你的

} else if(weight <= (month/3) +2 && weight >= ((5 * month)/12) +5) {

部分是错误的,因为这两个条件不能同时为真(例如,如果month 是 6,你会得到weight &lt;= 4 &amp;&amp; weight &gt;= 7.5,这对于任何weight 都是假的),所以如果打印@ 的条件987654325@ 为假,您将始终到达else 语句并打印"Invalid Weight!"

您可能希望其中至少一个为真(即 OR 条件):

} else if(weight <= (month/3) +2 || weight >= ((5 * month)/12) +5) {

【讨论】:

  • 这是我的问题,非常感谢!我刚开始使用java,我是一个疲惫的大学生。我完全错过了。让 or 条件解决了我的问题!
【解决方案2】:
import java.util.Scanner;


public class Test {

 public static void main(String[] args) {
     System.out.println("(1) months then weight in kg");

        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int selection = input.nextInt();
        switch(selection){
            case 1:
                    System.out.println("Enter age in months: ");
            double month = input.nextDouble(); 
                if(month >= 0 && month <= 24){   
                    System.out.println("Enter a weight in kg: ");
                } else{
                    System.out.println("Not a baby anymore");
                    System.exit(0);    
                }
            double weight = input.nextInt();
                if((weight >= 2) &&( weight <= 15 )&&( weight >= (month/3) +2 )&& (weight <= ((5 * month)/12) +5)) {
                    System.out.println("Healthy!");
                } else if((weight <= (month/3) +2 )&& (weight >= ((5 * month)/12) +5)) {
                    System.out.println("Not Healthy!");
                } else{
                    System.out.println("Invalid Weight!");
                }
                break;

       }
   }
}

输出:

/******execution in eclipse************/
/*
(1) months then weight in kg
Enter a number: 1
Enter age in months: 
0
Enter a weight in kg: 
3
Healthy!
*/

【讨论】:

  • 总是在括号中加上逻辑运算符;
猜你喜欢
  • 1970-01-01
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
  • 2015-06-21
  • 2011-11-16
  • 2012-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多