【问题标题】:display error message immediately when there are multiple if statements. my program displays error message after both are entered当有多个 if 语句时立即显示错误消息。输入两者后我的程序显示错误消息
【发布时间】:2018-03-22 07:43:38
【问题描述】:

如果用户输入了无效的体重或年龄,程序应该立即显示错误消息并结束程序。输入年龄和体重后,我的程序会显示一条错误消息。谁能帮我?示例:用户输入 1 用于 switch 语句以选择输入以月为单位的年龄和以 kg 为单位的体重。如果用户在月份输入 -5,它应该立即显示错误消息并结束程序。相反,我的程序所做的是在为年龄输入 -5 之后,它会提示用户然后输入体重。输入重量后,它会给我错误消息并结束我的程序。任何帮助都感激不尽。

 import java.util.Scanner;
 import java.lang.Math;
 public class infant{

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number;
double ageMonths = 0;
double ageYears = 0;
double weightPounds = 0;
double weightKg = 0;


System.out.println( "Choice 1 input the age in months, then weight in kilograms");          // this tells the user what the numbers 1-4 does when chosen
System.out.println( "Choice 2 input the age in years, weight in kilograms");
System.out.println( "Choice 3 input the age in months, weight in pounds");
System.out.println( "Choice 4 input the age in years, weight in pounds");


number =sc.nextInt();

if(number<=4 && number>=1){                                                  // if statement that makes the user enter a value from 1 to 4 for the switch statment.
        System.out.println("valid number selection");
    }else{ System.out.println("need to choose a number between 1 and 4");
        return;
    }

 switch(number){                                                                    // switch statment that allows the user to choose how they want to enter the height and weight 
    case 1: System.out.println("age in months: ");  ageMonths =sc.nextDouble();
            System.out.println("weight in kilograms: "); weightKg =sc.nextDouble();
            ageYears = ageMonths * 0.0833334;                                          // this switch statment also converts Kg to pounds and years to months in order for math equation in the if statement at then end of the program to work. 
            weightPounds = weightKg * 2.20462; 
            break;
    case 2: System.out.println("age in years: "); ageYears =sc.nextDouble();
            System.out.println("weight in kilograms: "); weightKg =sc.nextDouble();
            ageMonths = ageYears * 12; 
            weightPounds = weightKg * 2.20462; 
            break;
    case 3: System.out.println("age in months: "); ageMonths =sc.nextDouble();
            System.out.println("weight in pounds: "); weightPounds =sc.nextDouble();
            ageYears = ageMonths * 0.0833334;
            weightKg = weightPounds * 0.453592;
            break;
    case 4: System.out.println("age in years: "); ageYears =sc.nextDouble();
            System.out.println("weight in pounds: "); weightPounds =sc.nextDouble();
            ageMonths = ageYears * 12; 
            weightKg = weightPounds * 0.453592; 
            break;
    default: System.out.println("you entered in an invalid number"); 

}    

 if(ageMonths<=24 && ageMonths>-0.1){                                        // if statement that makes sure the ages are in the correct range
     System.out.println("valid age input");
    }else if(ageYears>-0.1 && ageYears<=2){
        System.out.println("valid age input");
    }else{ System.out.println("You entered a invaild age 0 to 24 months only");
        return;
}

    if(weightPounds>=4.40925 && weightPounds<=33.0693 || weightKg>=2 && weightKg<=15){      // if statement that makes sure the weight are in the correct range
        System.out.println("valid weight input");
    }else{ System.out.println("You entered a invaild weight 2kg to 15kg only");
        return;
    }




    if(weightKg >= ageYears * ageMonths / 3 + 2 && weightKg <= 5 * ageYears * ageMonths / 12 + 5){ // if statement that does a math equation to determine if your infant is healthy or not
        System.out.println(" your infant is healthy");
    }else{ System.out.println(" your infant is not healthy");
    }

     }
     }

【问题讨论】:

    标签: java if-statement java.util.scanner


    【解决方案1】:

    检查重量和月份的代码,你把代码保存在一些不同的函数中,然后在你输入月份后放在开关盒中,然后在输入月份的情况下调用函数,然后你可以做同样的事情权重的事情,这样你就可以拥有你想要的结构。请问你是否需要任何澄清。

    function checkMonths(double ageMonths){
    if(ageMonths<=24 && ageMonths>-0.1){                                        // if statement that makes sure the ages are in the correct range
     System.out.println("valid age input");
    }else{ System.out.println("You entered a invaild age 0 to 24 months only");
        return;
    }
    }
    function checkYears(double ageYears){
    if(ageYears>-0.1 && ageYears<=2){
        System.out.println("valid age input");
    }else{ System.out.println("You entered a invaild age 0 to 24 months only");
        return;
    }
    }
    
    
    function checkWeights(double weightPounds){
    
    if(weightPounds>=4.40925 && weightPounds<=33.0693 || weightKg>=2 && weightKg<=15){      // if statement that makes sure the weight are in the correct range
        System.out.println("valid weight input");
    }else{ System.out.println("You entered a invaild weight 2kg to 15kg only");
        return;
    }
    
     }
    

    请根据您的使用情况更改函数参数中的变量名称。

    【讨论】:

    • 所以基本上你的意思是我需要在我的 switch 语句中包含体重和年龄的 if 语句?
    • 是的,基本上就是这样。但是你不需要在所有情况下都复制检查部分,因为你可以形成一个函数。
    • 我尝试使用代码函数checkWeights(double weight){
    • 发生了什么??
    • 但它不会编译
    【解决方案2】:

    如果你真的想用无效输入结束你的程序,你总是可以使用 System.exit(0) 而不是“return”。这保证会结束程序。

    例如:

     if(number<=4 && number>=1){                                                  // if statement that makes the user enter a value from 1 to 4 for the switch statment.
        System.out.println("valid number selection");
    }else{ System.out.println("need to choose a number between 1 and 4");
        System.exit(0);
    }
    

    除非我弄错了,否则应该在 print 语句之后立即终止主线程。

    【讨论】:

      猜你喜欢
      • 2013-03-13
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多