• 【Java】BMI身体质量指数计算工具


    😏★,°:.☆( ̄▽ ̄)/$:.°★ 😏
    这篇文章主要介绍BMI身体质量指数计算工具的Java实现。
    学其所用,用其所学。——梁启超
    欢迎来到我的博客,一起学习,共同进步。
    喜欢的朋友可以关注一下,下次更新不迷路🥞

    😏1. 知识介绍

    BMI(Body Mass Index,身体质量指数),也称为体重指数,是一种常用的衡量成人人体肥胖程度的指标。它通过身高和体重之间的数值关系来评估一个人的体重是否适中。

    BMI的计算公式如下:

    BMI = 体重(kg)/ (身高(m) * 身高(m))
    
    • 1

    根据计算得到的BMI值,可以将人体的体重状况分为以下几个范围:

    BMI < 18.5:体重过轻
    18.5 <= BMI < 24:体重正常
    24 <= BMI < 28:超重
    BMI >= 28:肥胖
    
    • 1
    • 2
    • 3
    • 4

    😊2. Java终端程序

    在这里插入图片描述

    package org.example;
    
    import java.util.Scanner;
    
    public class Main {
        /*
         * main函数是程序的入口函数
         */
        public static void main(String[] args) {
    
            Scanner input = new Scanner(System.in);
    
            System.out.println("欢迎使用BMI计算器");
            System.out.print("请输入您的体重(kg):");
            double weight = input.nextDouble();
    
            System.out.print("请输入您的身高(m):");
            double height = input.nextDouble();
    
            double bmi = calculateBMI(weight, height);
            System.out.println("您的BMI指数为:" + bmi);
            System.out.println("BMI指数结果解读:");
            interpretBMI(bmi);
    
            input.close();
    
        }
    
        /**
         * BMI计算
         * @param weight
         * @param height
         * @return
         */
        public static double calculateBMI(double weight, double height) {
            return weight / (height * height);
        }
    
        /**
         * BMI解读
         * @param bmi
         */
        public static void interpretBMI(double bmi) {
            if (bmi < 18.5) {
                System.out.println("您的体重过轻");
            } else if (bmi < 24) {
                System.out.println("您的体重正常");
            } else if (bmi < 28) {
                System.out.println("您的体重超重");
            } else {
                System.out.println("您的体重肥胖");
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    😆3. Java-Swing界面程序

    可以使用 JTextFields 用于输入体重和身高,使用JButton 触发计算,并使用JLabel显示结果。

    在这里插入图片描述

    package org.example;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Dimension;
    
    public class Main extends JFrame {
        private JTextField weightField;
        private JTextField heightField;
        private JLabel resultLabel;
    
        public Main() {
            setTitle("BMI计算器");
            setSize(675, 120);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // 获取屏幕尺寸
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            int screenWidth = (int) screenSize.getWidth();
            int screenHeight = (int) screenSize.getHeight();
    
            // 计算窗口显示位置
            int x = (screenWidth - getWidth()) / 2;
            int y = (screenHeight - getHeight()) / 2;
            setLocation(x, y);
    
            // 创建面板
            JPanel panel = new JPanel();
            JLabel weightLabel = new JLabel("体重(kg):");
            weightField = new JTextField(10);
            JLabel heightLabel = new JLabel("身高(m):");
            heightField = new JTextField(10);
            JButton calculateButton = new JButton("计算");
            resultLabel = new JLabel();
    
            calculateButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    double weight = Double.parseDouble(weightField.getText());
                    double height = Double.parseDouble(heightField.getText());
                    double bmi = calculateBMI(weight, height);
                    String interpretation = interpretBMI(bmi);
                    resultLabel.setText("您的BMI指数为:" + bmi + ",结果解读:" + interpretation);
                }
            });
    
            panel.add(weightLabel);
            panel.add(weightField);
            panel.add(heightLabel);
            panel.add(heightField);
            panel.add(calculateButton);
            panel.add(resultLabel);
            add(panel);
        }
    
        private double calculateBMI(double weight, double height) {
            return weight / (height * height);
        }
    
        private String interpretBMI(double bmi) {
            if (bmi < 18.5) {
                return "您的体重过轻";
            } else if (bmi < 24) {
                return "您的体重正常";
            } else if (bmi < 28) {
                return "您的体重超重";
            } else {
                return "您的体重肥胖";
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Main().setVisible(true);
                }
            });
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    😆4. Java程序打包成jar

    文件-项目结构-工件中,添加工件:

    在这里插入图片描述

    基于模板创建jar

    在这里插入图片描述

    然后构建中选择构建工件,就会生成jar包到out目录了。然后在终端运行即可:

    java -jar xxx.jar
    
    • 1

    请添加图片描述

    以上。

  • 相关阅读:
    【无标题】发的东方人
    对于Mac OS10.1x合适安装的Typora版本合集
    网络拥塞控制的经济学原理
    HTML+CSS简单漫画网页设计成品 蜡笔小新3页 大学生个人HTML网页制作作品
    vue:生命周期函数总结
    【计算机网络实验】BGP配置实验
    kylin10 mount.nfs: No such device
    程序产生自我意识,创造人工生命
    Java 注解
    ABAP 调用HTTP上传附件(二)之中文乱码
  • 原文地址:https://blog.csdn.net/qq_40344790/article/details/134271099