题目要求
题目分析
题目要求较29题(是不是太胖了)相比,要求判断的是多个人的体重是否是在一定的范围之内。
将多条数据保存起来然后依次判断是很常见的,不做额外介绍。
需要注意的是绝对值的处理和单位的换算。输入的体重是市斤,根据身高计算的结果是公斤。
示例代码
import java.util.Scanner;
public class L1_031 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//要判断的总人数
int count = input.nextInt();
input.nextLine();
//保存身高的数组
int[] heights = new int[count];
//保存体重的数组
int[] weights = new int[count];
//接收输入行
String str = null;
//拆分输入的数据
String[] arr = new String[2];
//利用循环接收输入行并将数据拆分后保存到对应的数组当中
for(int i = 0; i < count; i++) {
str = input.nextLine();
arr = str.split(" ");
heights[i] = Integer.parseInt(arr[0]);
weights[i] = Integer.parseInt(arr[1]);
}
input.close();
//标准体重
double weight = 0;
//计算后的绝对值
double abs = 0;
for(int i = 0; i < count; i++) {
//根据身高计算标准体重
weight = (heights[i] - 100) * 0.9 * 2;
//绝对值
abs = Math.abs(weights[i] - weight);
//判断并输出结果
if(abs < weight * 0.1) {
System.out.println("You are wan mei!");
} else {
//如果不是完美体重 实际体重大于标准体重说明太胖 否则太瘦
if(weight < weights[i]) {
System.out.println("You are tai pang le!");
} else {
System.out.println("You are tai shou le!");
}
}
}
}
}