其实这就是从我学校的资源,都比较基础的算法题,先尽量每天都做1-2题,练手感。毕竟离我真正去尝试入职好的公司(我指的就是中大厂,但是任重道远啊),仍有一定的时间,至少要等我升本之后再说
题目
计算身体质量指数BMI及胖瘦程度判断。 任务描述:身体质量指数BMI(Body Mass Index ),简称体质指数,是国际上常用的衡量人体胖瘦 程度以及是否健康的一个标准。计算公式为:BMI=体重÷身高²。(体重单位:千克;身高单位:米。) 成人的BMI数值范围及胖瘦标准:BMI<18.5,过轻 ;18.5≤BMI<25,正常;25≤BMI<30,过重;BMI≥30, 肥胖。请输入n个同学的体重和身高,输出每个同学的BMI指数及胖瘦情况。(注:n<=10,BMI结果保留 两位小数)
代码
package suanfa;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;public class BMICalculator {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.print("输入学生人数: ");int n = scanner.nextInt();List<StudentInfo> studentInfos = new ArrayList<>();for (int i = 1; i <= n; i++) {System.out.println("输入第" + i + "个学生的体重和身高(体重kg 身高m):");double weight = scanner.nextDouble();double height = scanner.nextDouble();double bmi = Math.round(weight / (height * height) * 100.0) / 100.0;String status = getWeightStatus(bmi);// 存储学生信息studentInfos.add(new StudentInfo(weight, height, bmi, status));}// 输出表头System.out.println("体重\t身高\tBMI指数\t胖瘦情况");// 逐行输出每个学生的信息for (StudentInfo info : studentInfos) {System.out.printf("%.2f\t%.2f\t%.2f\t%s%n", info.weight, info.height, info.bmi, info.status);}scanner.close();}private static String getWeightStatus(double bmi) {if (bmi < 18.5) {return "过轻";} else if (bmi < 25) {return "正常";} else if (bmi < 30) {return "过重";} else {return "肥胖";}}static class StudentInfo {double weight;double height;double bmi;String status;StudentInfo(double weight, double height, double bmi, String status) {this.weight = weight;this.height = height;this.bmi = bmi;this.status = status;}}
}
结果