Reputation: 23
I am trying to use the variables from a static method named getBMI:
public static double getBMI(int weightKG, int heightCM)
{
Scanner input = new Scanner(System.in);
// Input Weight
System.out.print("Enter your weight in kilograms: ");
weightKG = input.nextInt();
System.out.println();
// Input Height
System.out.print("Enter your height in centimeters: ");
heightCM = input.nextInt();
System.out.println();
return heightCM;
return weightKG;
}
And use it in another static method named calculateMetricBMI:
public static void calculateMetricBMI()
{
getBMI();
System.out.println("A body mass index of 20-25 is considered \"normal\"");
double bmiMetric = weightKG/Math.pow(heightCM/100.0, 2);
System.out.print("Your BMI is " + bmiMetric);
}
However, I am getting an error when attempting to getBMI(); in the calculateMetricBMI.
EDIT:
Decided to add parameters to getBMI(); Now it shows getBMI(int weightKG, heightCM);
However I get this error:
'.class' expected
';' expected
';' expected
unexpected type required: value found: class
Upvotes: 2
Views: 202
Reputation: 3476
You need to call getBMI();
with two paremters.
Also, you cannot return 2 different variables, although that would probably result in a syntax error.
Another thing to watch out for is that you need to save what getBMI();
is sending you.
Such as: int weightKG = getBMI()
, and getBMI()
should only return one value, not two as you describe in your code.
Upvotes: 0
Reputation: 21975
You can not return two values at the same time in a method. However, I changed your code to return directly the result of your calculation.
Your method doesn't need parameters if you're going to initialize them inside of it. Just create them inside of your method.
If you're willing to use a method with parameters, when you call the method, you should of course do it using parameters
getBMI()
will not work here, you should've called getBMI(50.0, 165.0)
for example but this is a bad example cause it wouldn't have worked anyway.
Look at the code below, it is much clearer and will get you a better result.
public static double getBMIMetric()
{
Scanner input = new Scanner(System.in);
// Input Weight
System.out.print("Enter your weight in kilograms: ");
int weightKG = input.nextDouble();
System.out.print("Enter your height in centimeters: ");
int heightCM = input.nextInt();
System.out.println();
return weightKG/Math.pow(heightCM/100.0, 2);
}
public static void calculateMetricBMI()
{
System.out.println("A body mass index of 20-25 is considered \"normal\"");
double bmiMetric = getBMIMetric();
System.out.print("Your BMI is " + bmiMetric);
}
Upvotes: 0
Reputation: 3962
You're getting an error because you're calling getBMI() with nothing in the parameters, it needs to take 2 ints to be called.
You also can't return 2 variables from 1 method.
Try this:
public static void calculateMetricBMI() {
double weightKG = getWeight();
double heightCM = getHeight();
System.out.println("A body mass index of 20-25 is considered \"normal\"");
double bmiMetric = weightKG/Math.pow(heightCM/100.0, 2);
System.out.print("Your BMI is " + bmiMetric);
}
public static double getWeight() {
Scanner input = new Scanner(System.in);
// Input Weight
System.out.println("Enter your weight in kilograms: ");
double weightKG = input.nextInt();
return weightKG;
}
public static double getHeight() {
Scanner input = new Scanner(System.in);
// Input Height
System.out.println("Enter your height in centimeters: ");
double heightCM = input.nextInt();
return heightCM;
}
And in your main just call
calculateMetricBMI();
This is a pretty redundant solution, you can always just ask for the input in calculateMetricBMI() rather than having to call 2 other methods.
Upvotes: 1