在做一个BMI计算器的赋值时,我总是遇到编译器和所使用的方法的问题。
赋值要求我调用函数double bmi来计算bmi。我在正确调用函数时遇到了问题。任何帮助都是最好的。
其中一个错误是:
Prog5.java:44: error: illegal start of expression
public static double calculateBmi(double height, double total) {
^代码:
import java.util.Scanner;
public class Prog5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double avgweight,bmi,total,wReading;
int heightft,heightin,height,k;
String category,weightreading;
System.out.print("Enter the height(in feet and inches separated by spaces): ");
heightft = sc.nextInt();
heightin = sc.nextInt();
height = ((heightft*12)+heightin);
System.out.print("Enter the weight values separated by spaces followed by a negative number: ");
wReading = sc.nextDouble();
While (wReading >=0);
{
total = wReading+total;
Count++;
wReading = sc.nextDouble();
}
avgweight = 0;
total = 0;
weightreading = "Weight readings: " + wReading;
avgweight = total/Count;
public static double calculateBmi(double height, double total) {
{
double bmi = 0;
double total = 0;
double height = 0;
bmi = (height*703) / (total*total);
}
return bmi;
}
if ( bmi > 30)
category=("Obese");
else if (bmi >= 25)
category=("Overweight");
else if (bmi >= 18.5)
category=("Normal");
else {
category=("Underweight");
}
System.out.println("");
System.out.println("Height: "+ heightft + " feet " + heightin + " inches" );
System.out.println("Weight readings: "+ count);
System.out.println("Average weight: " + avgweight + "lbs");
System.out.println("");
System.out.printf("BMI: " + "%.2f", bmi);
System.out.println("");
System.out.println("Category: " + category);
System.out.println("");
}
private static void ElseIf(boolean b) { }
private static void If(boolean b) { }
}发布于 2014-04-16 08:58:49
您提到的问题是由于您在main中启动了另一个方法。相反,您需要一个类似于以下内容的结构:
public class Prog5
{
public static void main(String[] args)
{
// code here
}
public static double calculateBMI(double height, double total)
{
//other code
}
}发布于 2014-04-16 08:57:35
您的问题是您正试图在一个方法(public static void main)内定义一个方法(即public static double calculateBMi),而Java不允许您这样做。(基本上,不是main的方法需要附加到一个类。)
将来,你可能想在问这类问题之前四处看看,因为已经问了这个问题的重复版本。你的问题基本上是:Function within a function in Java
https://stackoverflow.com/questions/23097078
复制相似问题