Java 如何计算 BMI
How to Calculate BMI in Java
我正在编写一个程序,该程序接受用户输入的身高和体重,然后据此计算体重指数。它对身高、体重和 BMI 使用不同的方法,这些方法从 main 中调用。我遇到的问题是我完全不知道如何将体重和身高方法的输入输入到 BMI 方法中。代码如下所示:
public class BMIProj {
static Scanner input = new Scanner(System.in);
public static int heightInInches()
{
System.out.println("Input feet: ");
int x;
x = input.nextInt();
System.out.println("Input Inches: ");
int y;
y = input.nextInt();
int height = x * 12 + y;
return height;
}
public static int weightInPounds()
{
System.out.println("Input stone: ");
int x;
x = input.nextInt();
System.out.println("Input pounds ");
int y;
y = input.nextInt();
int weight = x * 14 + y;
return weight;
}
public static void outputBMI()
{
}
public static void main(String[] args) {
heightInInches();
weightInPounds();
outputBMI();
}
提前致谢。
您可以像这样将方法的输出分配给参数:
int weight = weightInPounds();
调用方法时,可以传入参数:
outputBMI(weight);
剩下的就看你了。
我建议你多学习 java,特别是变量、声明、初始化等。还要学习 class、构造函数等。
- 您需要 class 的字段来保存输入的变量
- 我创建了一个构造函数来初始化变量
- 如果您所做的只是为 class 字段赋值并输出信息,则无需 return 方法中的任何内容。
我行了屈膝礼,为您计算了体重指数
无论如何
public class BMIProj {
static Scanner input = new Scanner(System.in);
// Class vars
int height;
int weight;
double bmi;
//Constructor
public BMIPrj(){
//Initialize vars
height = 0;
weight = 0;
bmi = 0;
}
public static void heightInInches()
{
System.out.println("Input feet: ");
int x;
x = input.nextInt();
System.out.println("Input Inches: ");
int y;
y = input.nextInt();
int height = x * 12 + y;
return height;
}
public static void weightInPounds()
{
System.out.println("Input stone: ");
int x;
x = input.nextInt();
System.out.println("Input pounds ");
int y;
y = input.nextInt();
int weight = x * 14 + y;
return weight;
}
public static void outputBMI()
{
System.out.println("BMI: " + (( weight / height ) x 703));
}
public static void main(String[] args) {
heightInInches();
weightInPounds();
outputBMI();
}
我正在编写一个程序,该程序接受用户输入的身高和体重,然后据此计算体重指数。它对身高、体重和 BMI 使用不同的方法,这些方法从 main 中调用。我遇到的问题是我完全不知道如何将体重和身高方法的输入输入到 BMI 方法中。代码如下所示:
public class BMIProj {
static Scanner input = new Scanner(System.in);
public static int heightInInches()
{
System.out.println("Input feet: ");
int x;
x = input.nextInt();
System.out.println("Input Inches: ");
int y;
y = input.nextInt();
int height = x * 12 + y;
return height;
}
public static int weightInPounds()
{
System.out.println("Input stone: ");
int x;
x = input.nextInt();
System.out.println("Input pounds ");
int y;
y = input.nextInt();
int weight = x * 14 + y;
return weight;
}
public static void outputBMI()
{
}
public static void main(String[] args) {
heightInInches();
weightInPounds();
outputBMI();
}
提前致谢。
您可以像这样将方法的输出分配给参数:
int weight = weightInPounds();
调用方法时,可以传入参数:
outputBMI(weight);
剩下的就看你了。
我建议你多学习 java,特别是变量、声明、初始化等。还要学习 class、构造函数等。
- 您需要 class 的字段来保存输入的变量
- 我创建了一个构造函数来初始化变量
- 如果您所做的只是为 class 字段赋值并输出信息,则无需 return 方法中的任何内容。
我行了屈膝礼,为您计算了体重指数
无论如何
public class BMIProj {
static Scanner input = new Scanner(System.in);
// Class vars
int height;
int weight;
double bmi;
//Constructor
public BMIPrj(){
//Initialize vars
height = 0;
weight = 0;
bmi = 0;
}
public static void heightInInches()
{
System.out.println("Input feet: ");
int x;
x = input.nextInt();
System.out.println("Input Inches: ");
int y;
y = input.nextInt();
int height = x * 12 + y;
return height;
}
public static void weightInPounds()
{
System.out.println("Input stone: ");
int x;
x = input.nextInt();
System.out.println("Input pounds ");
int y;
y = input.nextInt();
int weight = x * 14 + y;
return weight;
}
public static void outputBMI()
{
System.out.println("BMI: " + (( weight / height ) x 703));
}
public static void main(String[] args) {
heightInInches();
weightInPounds();
outputBMI();
}