Correct the following source so it compiles and runs correctly: import java.util
ID: 3614402 • Letter: C
Question
Correct the following source so it compiles and runs correctly:import java.util.Scanner;
import javax.swing.JOptionPane;
public class Homework3
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
// input
double weight =JOptionPane.showInputDialog("Enter your weight: ", 0.0);
double height =JOptionPane.showInputDialog("Enter your height: ", 0.0);
// calculations
double bmi = weight * 703 / height /height;
String output;
if(bmi < 18.5)
{
output = "You are underweight.";
}
else if(bmi > 25)
{
output = "You are overweight";
}
else
{
output = "You have optimal weight.";
}
// output
JOptionPane.showMessageDialog(null,output);
}
}
Explanation / Answer
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Homework3
{
public static void main(String[] args)
{String input;
double weight,height,bmi;
String output;
Scanner keyboard = new Scanner(System.in);
//input
input= JOptionPane.showInputDialog(null,"Enter yourweight: ");
weight=Double.parseDouble(input);
input = JOptionPane.showInputDialog(null,"Enter yourheight: ");
height=Double.parseDouble(input);
//calculations
bmi =(weight * 703) / (height * height);
if(bmi < 18.5)
{
output = "You areunderweight.";
}
else if(bmi >25)
{
output = "You areoverweight";
}
else
{
output = "You haveoptimal weight.";
}
// output
JOptionPane.showMessageDialog(null,output);
}
}