gpt4 book ai didi

java - BMI计算器错误

转载 作者:行者123 更新时间:2023-12-01 13:08:19 24 4
gpt4 key购买 nike

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

}

最佳答案

您提到的问题是由于您在 main 中开始了另一个方法。相反,您想要一个类似的结构:

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

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

关于java - BMI计算器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23097078/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com