You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

整数转小数:BMI计算器中身高体重数值转换及计算疑问

解决BMI计算器的单位转换问题

你需要先把英尺+英寸、英石+磅转换成统一的浮点型数值,才能代入BMI公式计算,核心是搞对单位进制:

1. 身高转换(英尺+英寸 → 统一数值)

1英尺=12英寸,有两种常用转换方式:

  • 转成总英寸数(计算时自动转为浮点型):double heightInInches = feet * 12 + inches;
  • 转成带小数的英尺数:double heightInFeet = feet + inches / 12.0;(注意用12.0而非12,避免整数除法导致精度丢失)

2. 体重转换(英石+磅 → 统一数值)

1英石=14磅,对应两种转换方式:

  • 转成总磅数(计算时自动转为浮点型):double weightInPounds = stone * 14 + pounds;
  • 转成带小数的英石数:double weightInStone = stone + pounds / 14.0;

3. BMI计算的完整代码示例

结合你的输入代码,完整实现如下:

import java.util.Scanner;

public class BMICalculator {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        System.out.print("Enter feet: ");
        int feet = input.nextInt();
        
        System.out.print("Enter inches: ");
        int inches = input.nextInt();
        
        System.out.print("Enter stone: ");
        int stone = input.nextInt();
        
        System.out.print("Enter pounds: ");
        int pounds = input.nextInt();
        
        // 转换身高为总英寸数(匹配BMI公式的单位要求)
        double heightInInches = feet * 12 + inches;
        // 转换体重为总磅数(匹配BMI公式的单位要求)
        double weightInPounds = stone * 14 + pounds;
        
        // 计算BMI
        double bmi = weightInPounds * 703 / (heightInInches * heightInInches);
        
        // 格式化输出结果,保留两位小数
        System.out.printf("Your BMI is: %.2f%n", bmi);
        
        input.close();
    }
}

关键提醒

  • 不要用“两数相加除以10”的方式,英寸和英尺是12进制、磅和英石是14进制,和10无关,会导致数值完全错误。
  • printf仅用于格式化输出结果,计算BMI必须先把单位转成正确的浮点型数值,再代入公式计算。

内容的提问来源于stack exchange,提问作者user20758216

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 11:55:24