asked 16.4k views
3 votes
I need mega help ASAP. I need this code in java and I've attempted so many different codes and none work. Please help! This is what I have so far; the instructions are in the image below!

import java.util.Scanner;

public class MyProgram
{
public static void main(String[] args)
{

int num = 0;
Scanner input = new Scanner(System.in);

while(true){
try{
System.out.println("Enter a number.");
num = input.nextInt();
if(num <= 0){
System.out.println("Number must be greater than 0");
continue;
}
break;
}
catch(Exception e){
System.out.println("Print error message here");
input.nextLine();
}

}

//make calculations
//double bmi = 0;
//bmi = (704 * weight) / (height * height);

System.out.println("Program ended");
}
}

I need mega help ASAP. I need this code in java and I've attempted so many different-example-1

1 Answer

3 votes

Answer:

import java.util.Scanner;

public class MyProgram {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

// Prompt the user to enter their weight in kilograms.

System.out.println("Enter your weight in kilograms: ");

double weight = input.nextDouble();

// Prompt the user to enter their height in meters.

System.out.println("Enter your height in meters: ");

double height = input.nextDouble();

// Calculate the BMI.

double bmi = (weight * 703) / (height * height);

// Print the BMI.

System.out.println("Your BMI is " + bmi);

// Classify the BMI.

if (bmi < 18.5) {

System.out.println("You are underweight.");

} else if (bmi < 25) {

System.out.println("You have a normal weight.");

} else if (bmi < 30) {

System.out.println("You are overweight.");

} else if (bmi < 35) {

System.out.println("You are obese.");

} else {

System.out.println("You are severely obese.");

}

}

}

answered
User Dreyln
by
8.0k points