Discuss / Java / println没有格式化功能

println没有格式化功能

Topic source

alienation

#1 Created at ... [Delete] [Delete and Lock User]

要格式化只能printf

public class Hello {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Height (m): ");
        double height = scanner.nextDouble();
        System.out.print("Weight (kg): ");
        double weight = scanner.nextDouble();
        // FIXME:        String BMI = "";
        double bmi = weight/(height*height);
        if (bmi < 18.5) {
            BMI = "过轻";
        } else if (bmi <= 25) {
            BMI = "正常";
        } else if (bmi <= 28) {
            BMI = "过重";
        } else if (bmi <= 32) {
            BMI = "肥胖";
        } else if (bmi > 32) {
            BMI = "非常肥胖";
        } else {
            System.out.println("请输入合适的值");
        }
        System.out.printf("BMI=%s",BMI);
        // TODO: 打印BMI值及结果    }

}

if elseif esle

和if if if的区别是

前者是串联,只要满足了if条件后续判断不再执行

后者会一个一个执行所有的判断


  • 1

Reply