为何我的Java程序计算BMI始终返回Infinity?求排查逻辑错误
问题排查:BMI计算始终返回Infinity
我的程序通过BMI()方法计算患者的BMI,但无论输入什么合规值,返回结果都是Infinity。我知道数值除以0会返回Infinity,但已经对输入值做了非0限制,求帮忙找出逻辑错误。
原代码
import java.util.*; public class PDProjectOne { private static double weight, age, biximent, genFactor, bmi; private static String gender; public static double genderFactor(){ if(gender.equals("R") || (gender.equals("r"))){ genFactor = 8.46; } if(gender.equals("S") || (gender.equals("s"))){ genFactor = 4.23; } if(gender.equals("T") || (gender.equals("t"))){ genFactor = 6.35; } return genFactor; } public static double BMI(){ bmi = 2.56 * age + ((biximent * 467)/(weight * genFactor)); return bmi; } public static void report(){ Scanner sc = new Scanner(System.in); boolean good = false; do{ System.out.print("Weight in kilograms: "); weight = sc.nextDouble(); if (weight <= 28){ System.out.println("!! You must only enter a number greater than 28 kilograms. Try again. !!"); } else{ good = true; } } while (!good); good = false; do{ System.out.print("Age in solar cycles: "); age = sc.nextDouble(); if (age >= 16 && age <= 96){ good = true; } else{ System.out.println("!! You must only enter an age between 16 and 96 solar cycles. Try again. !!"); } } while (!good); good = false; do{ System.out.print("Gender identification: "); gender = sc.next(); if (gender.equals("R") || gender.equals("r") || gender.equals("S") || gender.equals("s") || gender.equals("T") || gender.equals("t")){ good = true; } else{ System.out.println("!! You must only enter 'R','S',or 'T' as gender identifications. Try again. !!"); } } while (!good); good = false; do{ System.out.print("Biximent level: "); biximent = sc.nextDouble(); if (biximent >= 30){ good = true; } else{ System.out.println("!! You must only enter a biximent level of at least 30. Try again. !!"); } } while (!good); System.out.println("\nSUMMARY OF ANALYSIS:\n" + "For a citizen of gender type " + gender + ", who is " + age + " cycles old and weighs " + weight + " kilograms with" + " a reported Biximent Level of " + biximent + ", the Biximent Metabolic Index (BMI) is " + BMI()); } }
问题根源
你定义了genderFactor()方法来计算genFactor,但从来没有调用过这个方法!
静态变量genFactor的默认初始值是0.0,所以在BMI()方法里的weight * genFactor其实是weight * 0,结果必然是0,导致后面的除法(biximent * 467) / 0直接返回Infinity。
你的输入校验只限制了weight、age、biximent的取值,但没处理genFactor未初始化的问题。
修复方案
方案1:在调用BMI()前手动触发genderFactor()
在report()方法的最后,调用BMI()之前,加一行调用genderFactor()的代码:
// 在打印摘要前先初始化genFactor genderFactor(); System.out.println("\nSUMMARY OF ANALYSIS:\n" + "For a citizen of gender type " + gender + ", who is " + age + " cycles old and weighs " + weight + " kilograms with" + " a reported Biximent Level of " + biximent + ", the Biximent Metabolic Index (BMI) is " + BMI());
方案2:修改BMI()方法,直接在内部获取genFactor
避免依赖静态变量的初始化,直接在BMI()里调用genderFactor()拿到值:
public static double BMI(){ double factor = genderFactor(); bmi = 2.56 * age + ((biximent * 467)/(weight * factor)); return bmi; }
额外优化建议
- 把
genderFactor()里的多个if改成else if,减少不必要的判断:
public static double genderFactor(){ if(gender.equalsIgnoreCase("R")){ genFactor = 8.46; } else if(gender.equalsIgnoreCase("S")){ genFactor = 4.23; } else if(gender.equalsIgnoreCase("T")){ genFactor = 6.35; } return genFactor; }
- 尽量避免使用过多静态变量,改成局部变量传递参数更安全,减少此类初始化问题。
内容的提问来源于stack exchange,提问作者PatPatis
相关产品推荐
相关产品推荐

