定义一个人类(类名Person),属性包括人类的姓名、年龄、身 高、体重,定义一个构造方法包括4个参数,通过参数实现对4个属性进行赋初值;定义一个方法check用来检验人的体重是否在正常范围之内,定义一个方法out用于输出个人资料(包括体重相关信息)。体重指数的计算公式如下: 体重指数=体重(公斤)÷(身高(米)的平方)kg/m2 正常体重:体重指数=18-25 偏瘦:体重指数<18 偏胖:体重指数>25 再定义一个Test类,在main方法中创建Person类的对象,然后调用check和out方法
import java.util.Scanner;
class Person {
String name;
int age;
double high;
int weight;
public Person(String name, int age, double high, int weight) {
super();
this.name = name;
this.age = age;
this.high = high;
this.weight = weight;
}
public Person() {
// TODO Auto-generated constructor stub
}
void check() {
double wt;
wt = weight / Math.pow(high, 2);
if (wt >= 18 && wt <= 25) {
System.out.println("体重正常");
} else if (wt < 18) {
System.out.println("体重偏瘦");
} else if (wt > 25) {
System.out.println("体重偏胖");
}
}
void out() {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("输入你的姓名:");
name = input.next();
System.out.println("输入你的年龄:");
age = input.nextInt();
System.out.println("输入你的身高:");
high = input.nextDouble();
System.out.println("输入你的体重:");
weight = input.nextInt();
}
}
public class Test {
public static void main(String[] args) {
Person person = new Person();
person.out();
person.check();
}
}
不知道你这个问题是否已经解决, 如果还没有解决的话: