string—如何在java中实现体重指数(bmi)

kh212irz  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(359)

我的问题是没有scanner方法和math.round和math.pow如何实现?
这是我的密码:

  1. import java.util.Scanner;
  2. public class BMI{
  3. public static void main(String args[]) {
  4. Scanner sc = new Scanner(System.in);
  5. System.out.print("Input weight in kilogram: ");
  6. double weight = sc.nextDouble();
  7. System.out.print("\nInput height in meters: ");
  8. double height = sc.nextDouble();
  9. double BMI = weight / (height * height);
  10. System.out.print("\nThe Body Mass Index (BMI) is " + BMI + " kg/m2");
  11. }
  12. }

我的另一个想法是它只是为了某个特定的价值。就我而言,重量75.0,尺码178.0

  1. public static void main(String args[]) {
  2. double weight = 75.0;
  3. double height = 178.0;
  4. double BMI = weight / (height * height);
  5. System.out.print("\nThe Body Mass Index (BMI) is " + BMI + " kg/m2");
  6. }
lokaqttq

lokaqttq1#

如何初始化参数取决于开发人员。
如果不想使用扫描仪的简单方法是直接添加。
初始化也可以来自各种数据源:数据库、文件(xml、文本)、web服务等。
出于学校的目的,也许你可以尝试建立一个bmi类,并使用构造函数来传递任何可能需要的参数。
使用带参数的构造函数的好处是,可以使用不同的结果(基于params)构建各种bmi示例,而不仅仅是所有类示例只有一个结果(因为输入是相同的)。
如:

  1. public class BMI
  2. {
  3. double BMI;
  4. public BMI(double weight,double height )
  5. {
  6. this.BMI = weight / (height * height);
  7. }
  8. public String toString()
  9. {
  10. return "\nThe Body Mass Index (BMI) is " + this.BMI + " kg/m2";
  11. }
  12. public static void main(String args[])
  13. {
  14. BMI test1 = new BMI(100,1.90);
  15. BMI test2 = new BMI(68.77,1.60);
  16. System.out.println(test1);
  17. System.out.println(test2);
  18. }
  19. }

输出:

  1. The Body Mass Index (BMI) is 27.70083102493075 kg/m2
  2. The Body Mass Index (BMI) is 26.863281249999993 kg/m2
展开查看全部

相关问题