简体   繁体   中英

I having trouble assigning strings in an if else statement

Right now I have a programming assignment to calculate bmi(body mass index) and assign it to a classificaiton. Depending on the bmi it assigns it as follows. I am having trouble with the classification getting the strings. When i try to put the classification in a message box it tells me I do not have it initialized.

            public static void main(String[] args) {
    //Variables
    double bmi;         // Body Mass Index
    String weight;      // Weight in kilograms
    String height;      // height in meters
    String classification;

    weight = JOptionPane.showInputDialog("Welcome to the BMI and BSA Calculator to begin enter weight in kilograms.");
    double weight2 = Double.parseDouble(weight);
    height = JOptionPane.showInputDialog("Enter height in meters");
    double height2 = Double.parseDouble(height);

    bmi = weight2/(height2*height2);

    JOptionPane.showMessageDialog(null, "Your BMI is: " + bmi);

    if (bmi < 18.5)
    {
        classification = "Underweight";
    }
    else if (bmi < 25)
    {
        classification = "Normal";
    }
    else if (bmi < 30)
    {
        classification = "Overweight";
    }
    else
    {
        classification = "Obese";
    }


    JOptionPane.showInternalMessageDialog(null, "Your classification is: " + classification);

It's not really an answer to your question, but you're missing some values in your if statements. For example, what if bmi was 24.95, that wouldn't enter either the Normal or Overweight blocks.

In general, I try to pick one type of comparison, and just use that.

if (bmi < 18.5)
{
    classification = "Underweight";
}
else if (bmi < 25)
{
    classification = "Normal";
}
else if (bmi < 30)
{
    classification = "Overweight";
}
else
{
    classification = "Obese";
}

As for the error you're getting, assuming classification is declared just above the if statement, it really shouldn't be giving you that error.

What happens if you change the last line to

String result = "Your result is: " + classification;        
JOptionPane.showInternalMessageDialog(null, result);

And why are you using showInternalMessageDialog instead of showMessageDialog as previously used.

It should work fine, assuming you've declared the variable somewhere. (That's in terms of compiling and running without crashing; as pointed out in comments, the actual BMI comparisons have problems, and the brackets around the string literals are redundant.)

For example:

public class Test
{
    public static void main(String[] args) 
    {
        double bmi = Double.parseDouble(args[0]);

        String classification;

        if (bmi < 18.5)
        {
            classification = ("Underweight");
        }
        else if (bmi >= 18.5  && bmi < 24.9)
        {
            classification = ("Normal");
        }
        else if (bmi >= 25 && bmi < 29.9)
        {
            classification = ("Overweight");
        }
        else
        {
            classification = ("Obese");
        }

        System.out.println("Classification: " + classification);
    }
}

Please show a similar short but complete program which demonstrates the problem.

要分配变量String值,请执行以下操作:

classification = new String("whatever");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM