BMI计算器错误

时间:2014-04-16 00:51:08

标签: java calculator

在为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) { }

}

2 个答案:

答案 0 :(得分:1)

您提到的问题是由于您在main内开始了另一种方法。你想要一个类似的结构:

public class Prog5
{
    public static void main(String[] args)
    {
        // code here
    }

    public static double calculateBMI(double height, double total)
    {
        //other code
    }
}

答案 1 :(得分:0)

您的问题是您正在尝试在方法(public static void main)中定义方法(即public static double calculateBMi),而Java不允许您这样做。 (基本上,主要的方法不需要附加到类中。)

将来,在询问此类问题之前,您可能希望环顾四周,因为已经询问了此类问题的重复版本。您的问题基本上是:Function within a function in Java