关于#java#的问题:如果这个程序中public String show()改为了public void show()该如何更改使它正确呢


import java.util.Scanner;
 
class BMI {
    private double height;
    private double weight;
 
    public BMI(double height, double weight) {
        super();
        this.height = height;
        this.weight = weight;
    }
 
    public double getBMI() {
        return weight / (height * height);
    }
 
    public String assessBMI(double bmi) {
        String s = new String();
        if (bmi < 18.5) {
            s = "Low weight";
        } else if (bmi >= 18.5 && bmi < 24) {
            s = "Normal weight";
        } else if (bmi >= 24 && bmi < 28) {
            s = "Overweight";
        } else {
            s = "Fat or Obesity";
        }
        return s;
    }
 
    public String show() {
        return assessBMI(getBMI());
    }
}
    public class TestBMI {
    public static void main(String args[])
    {
Scanner sc=new Scanner (System.in);
System.out.printf("Please enter height(m):\n");
double height=sc.nextDouble();
System.out.printf("Please enter weight(kg):\n");
double weight=sc.nextDouble();
BMI s1=new BMI(height,weight);
System.out.printf("height %.3f, weight %.3f kg,",height,weight);
System.out.printf("BMI is %.3f,%s",s1.getBMI(),s1.show());
sc.close();
}
}

如果这个程序中public String show()改为了public void show()该如何更改使它正确呢

修改如下:

/**
 * @author huazie
 * @version 2.0.0
 * @since 2.0.0
 */

import java.util.Scanner;

class BMI {
    private double height;
    private double weight;

    public BMI(double height, double weight) {
        super();
        this.height = height;
        this.weight = weight;
    }

    public double getBMI() {
        return weight / (height * height);
    }

    public String assessBMI(double bmi) {
        String s;
        if (bmi < 18.5) {
            s = "Low weight";
        } else if (bmi >= 18.5 && bmi < 24) {
            s = "Normal weight";
        } else if (bmi >= 24 && bmi < 28) {
            s = "Overweight";
        } else {
            s = "Fat or Obesity";
        }
        return s;
    }

    public void show() {
        System.out.printf("%s",assessBMI(getBMI()));
    }
}

public class TestBMI {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.printf("Please enter height(m):\n");
        double height = sc.nextDouble();
        System.out.printf("Please enter weight(kg):\n");
        double weight = sc.nextDouble();
        BMI s1 = new BMI(height, weight);
        System.out.printf("height %.3f, weight %.3f kg,", height, weight);
        System.out.printf("BMI is %.3f,", s1.getBMI());
        s1.show();
        sc.close();
    }
}
不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^