private static void practiceIf(){ Scanner scanner = new Scanner(System.in); System.out.println("Please input your weight(Kg):"); String weightValue = scanner.nextLine(); System.out.println("Please input your height(m):"); String heightValue = scanner.nextLine(); double weight = 0.0; double height = 0.0; try { weight = Double.parseDouble(weightValue); } catch (NumberFormatException e) { e.printStackTrace(); System.out.printf("Invalid weight ‘%s’",weightValue); return; } try { height = Double.parseDouble(heightValue); } catch (NumberFormatException e) { e.printStackTrace(); System.out.printf("Invalid height ‘%s’",heightValue); return; } double bmi = weight/Math.pow(height, 2); System.out.printf("Your bmi is %.2f", bmi); String levelMessage = "OK"; if (bmi < 18.5){ levelMessage = "TOO LIGHT"; }else if (bmi >= 18.5 && bmi <= 25){ levelMessage = "NORMAL"; }else if (bmi > 25 && bmi <= 28){ levelMessage = "TOO HEAVY"; }else if (bmi > 28 && bmi <= 32){ levelMessage = "FAT"; }else if (bmi > 32){ levelMessage = "TOO FAT"; } System.out.printf(" and it's %s \n", levelMessage); }
输出:
Please input your weight(Kg): 71 Please input your height(m): 1.88 Your bmi is 20.09 and it's NORMAL Process finished with exit code 0
Sign in to make a reply
EMSonline_499
输出: