Java If-Else program error
Hello guys, I'm writing a Java program for my class but I couldn't make it to work. I tried everything and even ask a professor but still it doesn't work. I looked for how it is done on youtube or here but the more I search, the more I get confused. So.. I'm really needing some help please.
My program consists of two classes - Lab1main.java and Healthprofile.java. Lab1main.java is the primary class where it prompts the user for name, age, height in feet, height in inches, and weight in pounds. Here also where the input Name and Age, as well as, calculated values of Maximum Heart Rate (MaxHR), Body Mass Index (BMI), and Body Mass Index Category (BMIcat) are called upon for output display. Everything works fine in this program. The program is below..
package healthprofile;
import java.util.Scanner;
public class Lab1Main
{
public static void main(String[] args)
{
// set up scanner for user input
Scanner input = new Scanner(System.in);
// declare variables
int myage;
double myweight, myfeet, myinch;
String myname;
//instantiate
HealthProfile box = new HealthProfile();
// prompt user for age
System.out.print("Enter name or X to quit: " + box.getName());
myname = input.next();
System.out.print("Your age: " + box.getAge());
myage = input.nextInt();
System.out.print("Your weight in lbs: " + box.getWeight());
myweight = input.nextDouble();
System.out.print("Your height - feet: " + box.getFeet());
myfeet = input.nextDouble();
System.out.print("Your height - inches: " + box.getInch());
myinch = input.nextDouble();
//store user input in the object
box.setName(myname);
box.setAge(myage);
box.setWeight(myweight);
box.setFeet(myfeet);
box.setInch(myinch);
//output
System.out.println("Health Profile for " + box.getName());
System.out.println("BMI: " + box.getBMI());
System.out.println("Max heart rate: " + box.getMaxHR());
System.out.println("BMI Category: " + box.getBMIcat());
} // end main
} // end class Lab1Main
The secondary class is HealthProfile.java where the user input values are stored and calculated. Here is where I'm having errors under "public String BMIcat()". The error I'm getting is "Illegal character" in my If-Else statements. This part of the program has to calculate for Body Mass Index (bmic) and then return String values of whether the person is "Underweight", "Normal", "Overweight", or "Obese". The program is shown below.
package healthprofile;
public class HealthProfile
{
//attributes
private String name;
private int age;
private double weight;
private double feet, inch;
//constructor
public HealthProfile()
{
/* name = null;
age = 0;
weight = feet = inch = 0;*/
}
// get methods
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public double getWeight()
{
return weight;
}
public double getFeet()
{
return feet;
}
public double getInch()
{
return inch;
}
//public void
public void setName(String nam)
{
name = nam;
}
public void setAge(int ag)
{
age = ag;
}
public void setWeight(double wei)
{
weight = wei;
}
public void setFeet(double fet)
{
feet = fet;
}
public void setInch(double inc)
{
inch = inc;
}
public double getBMI()
{
//calculate and return BMI (Body Mass Index)
double bmi = (weight*703)/(((feet*12)+inch)*((feet*12)+inch));
return bmi;
}
public double getMaxHR()
{
//calculate and return Max Heart Rate
return 200 - age;
}
public String getBMIcat()
{
//calculate and return BMI Category
String bmicat = "";
double bmic = (weight*703)/(((feet*12)+inch)*((feet*12)+inch));
if (bmic < 18.5)
{
bmicat = "Underweight";
}
else if (bmic >= 18.5 && bmic <= 24.9)
{
bmicat = "Normal";
}
else if (bmic >= 24.9 && bmic <= 29.9)
{
bmicat = "Overweight";
}
else if (bmic >= 29.9 )
{
bmicat = "Obese";
}
return bmicat;
} // end public String BMIcat()
} // end HealthProfile
I've been trying to figure out the problem even ask a professor but somehow I'm still getting errors. Please help.. I don't know what else is missing. I'll really appreciate your help. Thanks
Aucun commentaire:
Enregistrer un commentaire