【发布时间】:2015-09-27 22:48:18
【问题描述】:
我正在尝试使用名为 getBMI 的静态方法中的变量:
public static double getBMI(int weightKG, int heightCM)
{
Scanner input = new Scanner(System.in);
// Input Weight
System.out.print("Enter your weight in kilograms: ");
weightKG = input.nextInt();
System.out.println();
// Input Height
System.out.print("Enter your height in centimeters: ");
heightCM = input.nextInt();
System.out.println();
return heightCM;
return weightKG;
}
并在另一个名为 calculateMetricBMI 的静态方法中使用它:
public static void calculateMetricBMI()
{
getBMI();
System.out.println("A body mass index of 20-25 is considered \"normal\"");
double bmiMetric = weightKG/Math.pow(heightCM/100.0, 2);
System.out.print("Your BMI is " + bmiMetric);
}
但是,我在尝试 getBMI(); 时遇到错误。在calculateMetricBMI中。
编辑:
决定给getBMI()添加参数; 现在它显示 getBMI(int weightKG, heightCM);
但是我得到了这个错误:
'.class' 预期
';'预计
';'预计
意外的类型 必需:值 找到:类
【问题讨论】:
-
您遇到的具体错误是什么,请您将其发布在问题上吗?
-
方法不能两次返回值。此外,您可能应该将方法返回的结果存储在某处。
-
此链接可能会有所帮助。 stackoverflow.com/questions/457629/…
-
编写一个方法来读取每个输入并返回单个值。这些方法应该使用 Scanner。然后在计算值时使用结果,可选择将它们传递给离散计算函数。静态变量(此处不存在)没有保证。
-
如果你想让别人帮你做作业,至少要选择一个答案。