簡體   English   中英

Java IF-ELSE IF-ELSE 跳過 if 和 else if 檢查並自動打印出 else 語句

[英]Java IF-ELSE IF-ELSE skipping the if and else if check and prints out else statement automatically

Java 11.6 在這個 BMI 計算器中,它將計算人的體重、身高並計算 BMI。 BMI 計算正確,但是在分類 BMI 方法中,程序跳過if 和 else if檢查,只打印出每次測試“肥胖”的 else 語句。 循環執行不正確嗎? 或者 BMI 的值不在循環中啟動。 當我打印出 BMI 時,我確實看到每次測試的 BMI 都會發生變化,因此這不是問題。

PersonWeight.java 類

import java.time.Year;


public class PersonWeight {


    private double height;
    private double weight;

    public PersonWeight() {

        height = 0;
        weight = 0;

    }

    public PersonWeight(double h, double w) {

        height = h;
        weight = w;

    }



    public void setHeight(double h) {
        this.height = h;
    }

    public double getHeight() {
        return height;
    }

    public void setWeight(double w) {
        this.weight = w;
    }

    public double getWeight() {
        return weight;
    }



    public double ComputeBMI() {

        double bmi = ((weight)/(height*height));
        return bmi;
    }



}

具有 main 方法的測試類

import java.util.Scanner;
public class TestPersonWeight {



    public static void classifyBMI() {
        PersonWeight PersonWeight = new PersonWeight();
        double bmi = PersonWeight.ComputeBMI();


        if(bmi<18.5) {
            System.out.printf("Underweight");

        }else if (bmi >= 18.5 && bmi<25) {
            System.out.printf("Normal Weight");
        }else if (bmi >=25 && bmi<30) {
            System.out.printf("Overweight");
        }else  {
            System.out.printf("Obese");

        }
    }

        public static void main(String[] args){

        Scanner input = new Scanner(System.in);
        TestPersonWeight TestPersonWeight = new TestPersonWeight();
        PersonWeight PersonWeight = new PersonWeight()
        System.out.printf("Enter person's Height in Meters: ");
        double h = input.nextDouble();
        PersonWeight.setHeight(h);


        System.out.printf("Enter person's Weight in Kilograms: ");
        double w = input.nextDouble();
        PersonWeight.setWeight(w);

        PersonWeight.ComputeBMI();
        System.out.printf("%n Height: " + PersonWeight.getHeight());
        System.out.printf("%n Weight: " + PersonWeight.getWeight());
        System.out.printf("%n BMI: " + PersonWeight.ComputeBMI());
        classifyBMI();

    }
}

您的classifyBMI() PersonWeight classifyBMI()方法創建了一個新的PersonWeight ,其中身高和體重保持為0.0 ,因此您將0.0除以0.0 ,結果為NaN 因此,所有比較的計算結果都不是true ,最終會執行 else 子句。

您應該將該方法更改為實例方法(即不是static ),並為您在main方法中創建的PersonWeight實例調用它。

或者,作為替代方法,保持該方法static ,但將先前計算的bmi值傳遞給它。

即在你的main寫作中:

double bmi = PersonWeight.ComputeBMI();
System.out.printf("%n Height: " + PersonWeight.getHeight());
System.out.printf("%n Weight: " + PersonWeight.getWeight());
System.out.printf("%n BMI: " + bmi);
classifyBMI(bmi);

並將classifyBMI為:

public static void classifyBMI (double bmi) {
    if(bmi < 18.5) {
        System.out.printf("Underweight");
    } else if (bmi >= 18.5 && bmi < 25) {
        System.out.printf("Normal Weight");
    } else if (bmi >= 25 && bmi < 30) {
        System.out.printf("Overweight");
    } else {
        System.out.printf("Obese");
    }
}

PS 對您的類名和變量名使用相同的標識符 - PersonWeight - 是一種不好的做法。 使用personWeight作為變量。

錯誤在這里:

PersonWeight personWeight = new PersonWeight();
double bmi = personWeight.computeBMI();

這總是返回 NaN(不是數字),因為您在一個只有零的新對象上進行計算。 不允許被零除,因此結果為 NaN。 所有 if 比較都失敗,因為 NaN 無法與任何數字進行比較。

解決方案 1: method inside the PersonWeight 類中移動 ``classifyBMI() method inside the並使用當前類實例屬性(現在是一個帶有零的新類)進行計算。

解決方案2:將填充的PersonWeight``` instance to the作為參數傳遞PersonWeight``` instance to the classifyBMI()``` 方法,並將其用於計算。

解決方案 3:將bmi值作為參數傳遞給 ``classifyBMI()``` 方法,並將其用於計算。

以下代碼演示了所有三種解決方案:

import java.util.Scanner;

public class Main
{

    public static class PersonWeight
    {
        private double height;
        private double weight;

        public PersonWeight()
        {
            height = 0;
            weight = 0;
        }

        public void setHeight(double h)
        {
            this.height = h;
        }

        public double getHeight()
        {
            return height;
        }

        public void setWeight(double w)
        {
            this.weight = w;
        }

        public double getWeight()
        {
            return weight;
        }

        public double computeBMI()
        {
            double bmi = ((weight) / (height * height));
            return bmi;
        }

        private void classifyBMI1()
        {
            double bmi = ((weight) / (height * height));

            if (bmi < 18.5)
            {
                System.out.println("Underweight");
            }
            else if (bmi >= 18.5 && bmi < 25)
            {
                System.out.println("Normal Weight");
            }
            else if (bmi >= 25 && bmi < 30)
            {
                System.out.println("Overweight");
            }
            else
            {
                System.out.println("Obese");
            }
        }
    }

    private static void classifyBMI2(PersonWeight personWeight)
    {
        double bmi = personWeight.computeBMI();
        if (bmi < 18.5)
        {
            System.out.println("Underweight");
        }
        else if (bmi >= 18.5 && bmi < 25)
        {
            System.out.println("Normal Weight");
        }
        else if (bmi >= 25 && bmi < 30)
        {
            System.out.println("Overweight");
        }
        else
        {
            System.out.println("Obese");
        }
    }

    private static void classifyBMI3(double bmi)
    {
        if (bmi < 18.5)
        {
            System.out.println("Underweight");
        }
        else if (bmi >= 18.5 && bmi < 25)
        {
            System.out.println("Normal Weight");
        }
        else if (bmi >= 25 && bmi < 30)
        {
            System.out.println("Overweight");
        }
        else
        {
            System.out.println("Obese");
        }
    }

    public static void main(String[] args) throws Exception
    {
        Scanner input = new Scanner(System.in);
        PersonWeight personWeight = new PersonWeight();

        System.out.printf("Enter person's Height in Meters: ");
        double h = input.nextDouble();
        personWeight.setHeight(h);

        System.out.printf("Enter person's Weight in Kilograms: ");
        double w = input.nextDouble();
        personWeight.setWeight(w);

        System.out.printf("%nHeight: %f%n", personWeight.getHeight());
        System.out.printf("Weight: %f%n", personWeight.getWeight());
        System.out.printf("BMI: %f2%n", personWeight.computeBMI());

        personWeight.classifyBMI1();
        classifyBMI2(personWeight);
        classifyBMI3(personWeight.computeBMI());
    }
}

我還修復了其他事情:

變量名和方法名應該以小寫字母開頭。 為類名保留大寫開頭。 要打印簡單的字符串,請使用println()而不是printf()以獲得更好的性能。 當您使用printf()輸出變量時,請使用%f等占位符而不是字符串連接。 這再次提供了更好的性能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM