Java黑皮书课后题第10章:10.2(BMI类)将下面的新构造方法加入BMI类中
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第10章:10.2(BMI类)将下面的新构造方法加入BMI类中
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Java黑皮書課后題第10章:10.2(BMI類)將下面的新構(gòu)造方法加入BMI類中
- 題目
- 程序說明
- 題目槽點(diǎn)
- 代碼:Test2_BMI.java
- 運(yùn)行實(shí)例
題目
程序說明
Test2_BMI.java:BMI類程序
這里我們參考編程練習(xí)題3.6的代碼
題目槽點(diǎn)
題目內(nèi)容表述的非常簡略,簡略得已經(jīng)有很多地方省略了
剛開始根本搞不清楚出題人到底想干什么
后來才逐漸意識到應(yīng)該把給出的構(gòu)造方法和測試程序放在一起
我個(gè)人想到在前面本來也有求BMI的題,所以用到3.6的內(nèi)容
代碼:Test2_BMI.java
import java.util.Scanner;public class Test2_BMI {double weight, height_feet, height_inches;String name;int age;private static double KILOGRAMS_PER_POUND = 0.45359237;private static double METERS_PER_FEET = 0.3048;private static double METERS_PER_INCH = 0.0254;public Test2_BMI(String name, int age, double weight, double feet, double inches){this.age = age;this.name = name;this.weight = weight;height_feet = feet;height_inches = inches;}public double getBMI(){// Compute BMIdouble weightInKilograms = weight * KILOGRAMS_PER_POUND;double heightInMeters = height_feet * METERS_PER_FEET + height_inches * METERS_PER_INCH;return weightInKilograms / (Math.pow(heightInMeters,2));}public static void main(String[] args){Scanner input = new Scanner(System.in);// Prompt the user to enter weight in poundsSystem.out.print("Enter weight in pounds: ");double weight = input.nextDouble();// Prompt the user to enter height in feetSystem.out.print("Enter feet: ");double height_feet = input.nextDouble();// Prompt the user to enter height in inchesSystem.out.print("Enter inches: ");double height_inches = input.nextDouble();System.out.print("Enter name: ");String name = input.next();System.out.print("Enter age: ");int age = input.nextInt();Test2_BMI tb = new Test2_BMI(name, age, weight, height_feet, height_inches);double bmi = tb.getBMI();// Display resultSystem.out.println("BMI is "+bmi);if(bmi < 18.5)System.out.println("Underweight");else if(bmi < 25)System.out.println("Normal");else if(bmi < 30)System.out.println("Overweight");elseSystem.out.println("Obese");} }運(yùn)行實(shí)例
Enter weight in pounds: 140 Enter feet: 5 Enter inches: 10 Enter name: liming Enter age: 20 BMI is 20.087702275404553 Normal總結(jié)
以上是生活随笔為你收集整理的Java黑皮书课后题第10章:10.2(BMI类)将下面的新构造方法加入BMI类中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第10章:*10.1
- 下一篇: java美元兑换,(Java实现) 美元