Java BMI计算器问题 - 英制或公制用户输入,错误处理,正确值输入,转换

时间:2017-06-08 13:54:32

标签: java error-handling user-input

我目前正在开发我的第一个Java程序作为初学程序员,但是,我对它的某些部分感到困惑。我看到有很多BMI计算器源代码,但大多数都非常简单。   但是,我的任务需要输入英制或公制值,将输入从公制转换为英制,反之亦然,错误处理和用户输入验证。我甚至不想尝试使用GUI,所以如果有人能给我一些简单的程序方向,我将非常感激。 到目前为止,这是我的代码(我希望它不是一团糟):

import java.util.Scanner;

public class BodyMassIndex {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in); 

       double weightKg, weightPounds, weightStones, heightCm, heightMeters, 
               heightFeet, heightInches, bMIMetric, bMIImperial;

       System.out.println("Please, enter your weight in kilograms: ");
       weightKg = input.nextDouble();
       System.out.println("Please enter your height in centimeters: ");
       heightCm = input.nextDouble(); 

       String correctValues1;
       if (weightKg < 20 && weightKg > 400) {
           System.out.println("Please enter valid weight.");
           correctValues1 = "Please enter valid weight.";
       }
       if (heightCm < 20 && heightCm > 250) {
           System.out.println("Please enter valid height.");
           correctValues1 = "Plase enter valid height.";
       }

       System.out.println("Please, enter your weight in pounds: ");
       weightPounds = input.nextDouble();
       System.out.println("Please enter your height in inches: ");
       heightInches = input.nextDouble();

       String correctValues2;
       if (weightPounds < 44 && weightPounds > 882) {
       System.out.println("Please enter valid weight.");
       correctValues2 = "Please enter valid weight.";
       }
       if (heightInches < 7 && heightInches > 98) {
       System.out.println("Please enter valid height.");
       correctValues2 = "Please enter valid height.";
       }

       heightMeters = heightCm/100;
       weightPounds = weightKg*0.453592;
       weightStones = weightKg*6.35029318;
       heightFeet = heightCm*0.0328084;
       heightInches = heightCm*0.393701;

       bMIMetric = (weightKg/(heightMeters*heightMeters));

       bMIImperial = (weightPounds/(heightInches*heightInches))*703;

    System.out.printf("Height in meters is %.2f\n", heightMeters);
    System.out.printf("Weight in pounds is %.2f\n", weightPounds);
    System.out.printf("Weight in stones is %.2f\n", weightStones);
    System.out.printf("Height in feet is %.2f\n", heightFeet);
    System.out.printf("Height in inches is %.2f\n", heightInches);

    String Result1;
            if (bMIMetric < 18.5) {
            System.out.println("less than 18.5");
            Result1 = "Underweight";
            } else if ((bMIMetric) >= 18.5 && (bMIMetric) <= 24.9) {
            System.out.println("between 18.5 and 24.9");
            Result1 = "Normal";
            } else if (bMIMetric >= 25 && bMIMetric <= 29.9) {
            System.out.println("between 25 and 29.9");
            Result1 = "Overweight";
            } else {
            System.out.println("greater than 30");
            Result1 = "Obese";
            }

    String Result2;
            if (bMIImperial < 18.5) {
            System.out.println("less than 18.5");
            Result2 = "Underweight";
            } else if ((bMIImperial) >= 18.5 && (bMIImperial) <= 24.9) {
            System.out.println("between 18.5 and 24.9");
            Result2 = "Normal";
            } else if (bMIImperial >= 25 && bMIImperial <= 29.9) {
            System.out.println("between 25 and 29.9");
            Result2 = "Overweight";
            } else {
            System.out.println("greater than 30");
            Result2 = "Obese";
            }

    System.out.println("Your Body Mass Index is:" + bMIMetric + "(" + Result1 +")");
    System.out.println("Your Body Mass Index is:" + bMIImperial +"(" + Result2 +")");

        }
    }

2 个答案:

答案 0 :(得分:1)

你正试图让它变得复杂,这实际上并不容易。 因此,您需要制作冗余和非逻辑步骤。

<强>输入 你要求不同指标的双输入。例如,重量以千克和磅为单位。您需要询问用户他使用的是哪个指标并从那里开始。

错误检查 我建议使用try / catch块并查找非数值

try {
    // get your input
} catch(InputMismatchException ex) {
    // try again
}

希望这有点帮助

答案 1 :(得分:-1)

以下内容的意图很棒 - 检查以确保输入的参数是正确的。但是,它只进行一次错误检查,并且不向用户提供正确的值。 (它也应该是一个或者,而不是,因为现在这个条件永远不会是真的。)

   String correctValues2;
       if (weightPounds < 44 && weightPounds > 882) {
       System.out.println("Please enter valid weight.");
       correctValues2 = "Please enter valid weight.";
       }

我会建议更多的内容:

     while (weightPounds < 44 || weightPounds > 882) {
           System.out.println("Please enter valid weight.(44 lb - 882 lb)");
           weightPounds = input.nextDouble();
           }

通过这种方式,您可以避免需要正确的价值&#39;在退出while循环时确保该值正确。

此外,如果程序应该指定用户是否想要公制或英制的BMI,请先询问。

顺便说一句,这篇文章可能属于Code Review以供将来参考