现在这个问题可能在此之前被问过,但我找不到了。所以我道歉。
出于某种原因,我的代码似乎运行了两次,即使通过代码我只要求输入一次。有人可以指导我搞错吗?
感谢。
import java.util.Scanner;
public class BMI
{
static Scanner kybd = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("Height in inches: " + heightInInches());
System.out.println("Weight in pounds: " + weightInPounds());
System.out.println(outputBMI(heightInInches(),weightInPounds()));
}
public static int heightInInches()
{
System.out.print("Enter your height in feet: ");
int feet = kybd.nextInt();
//feet validation
while(feet < 2 || feet > 7)
{
System.out.println("Input not vaild.");
System.out.print("Enter your height in feet: ");
feet = kybd.nextInt();
}
System.out.print("Enter your height in inches: ");
int inches = kybd.nextInt();
//inches validation
while(inches < 0 || inches > 13){
System.out.println("Input not vaild.");
System.out.print("Enter your height in inches: ");
inches = kybd.nextInt();
}
int totalInches = inches + (feet * 12);
return totalInches;
}
public static int weightInPounds()
{
System.out.print("Enter your weight in stone: ");
int stone = kybd.nextInt();
//stone validation
while(stone < 3 || stone > 30)
{
System.out.println("Input invalid.");
System.out.print("Enter your height in stone: ");
stone = kybd.nextInt();
}
System.out.print("Please enter your weight in pounds: ");
int pounds = kybd.nextInt();
//pounds validation
while(pounds < 0 || pounds > 30)
{
System.out.println("Input invalid.");
System.out.print("Please enter your weight in pounds: ");
pounds = kybd.nextInt();
}
int totalPounds = pounds + (stone * 14);
return totalPounds;
}
public static double outputBMI(double height, double weight)
{
double BMI = (weight * 703)/(height/height);
return BMI;
}
}
答案 0 :(得分:0)
正如评论中提到的那样,您需要只调用一次Helper!A1
和heightInInches
并将值存储在main方法的某些局部变量中,然后在需要的地方重用这些变量。例如,您可以像这样更新主要方法:
weightInPounds