-
[SOLVED] If's
:confused:
I have put together a piece of code
if
else
The first three parts work fine, however once we get passed the first three
Code:
if ((outputBmi < 18.5) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
txtResults.setText(" Underweight");
txtResults.setForeground(Color.ORANGE);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi <= 23) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
txtResults.setText(" OK");
txtResults.setForeground(Color.GREEN);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi > 23) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
txtResults.setText(" Overweight");
txtResults.setForeground(Color.RED);
txtResults.setBackground(Color.BLACK);
It all starts to run amuck
by purely changing the f to an F I can get a totally differant reading in the BMI.......
Like I say all the if's are layed out exactly the same, I cannot see the error.
Hopefully someone can point it out to me.
Thanks in advance.
Code:
if ((outputBmi < 18.5) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
txtResults.setText(" Underweight");
txtResults.setForeground(Color.ORANGE);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi <= 23) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
txtResults.setText(" OK");
txtResults.setForeground(Color.GREEN);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi > 23) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
txtResults.setText(" Overweight");
txtResults.setForeground(Color.RED);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi < 19.1) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 29)) )) {
txtResults.setText(" Underweight");
txtResults.setForeground(Color.ORANGE); // check bmi readings
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi <= 25.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 29)) )) {
txtResults.setText(" OK");
txtResults.setForeground(Color.GREEN);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi > 25.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 29)) )) {
txtResults.setText(" Overweight");
txtResults.setForeground(Color.RED);
txtResults.setBackground(Color.BLACK);
}
if ((outputBmi < 24) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 39)) )) {
txtResults.setText(" Underweight");
txtResults.setForeground(Color.ORANGE);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi <= 25.9) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 39)) )) {
txtResults.setText(" OK");
txtResults.setForeground(Color.GREEN);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi > 25.9) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 39)) )) {
txtResults.setText(" Overweight");
txtResults.setForeground(Color.RED);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi < 19.1) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 49)) )) {
txtResults.setText(" Underweight");
txtResults.setForeground(Color.ORANGE);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi <= 26.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 49)) )) {
txtResults.setText(" OK");
txtResults.setForeground(Color.GREEN);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi > 26.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 49)) )) {
txtResults.setText(" Overweight");
txtResults.setForeground(Color.RED);
txtResults.setBackground(Color.BLACK);
}
}
}
}}}}}}}}}
-
It seems condition given by you is not correct.
I think you want to check
BMI && (gender should be F || f) && age
So every condition can be changed as follows
Code:
if ((outputBmi <= 23) && ((aGender.equals("F")) || (aGender.equals("f"))) && (age <= 19))
Also seems you missed an else in between the code
Code:
txtResults.setBackground(Color.BLACK);
} [B]--->else{[/B]
if ((outputBmi < 24) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 39)) )) {
Hope this helps you.
-
Thanks, the fact that I had missed the else statement out was the issue, everything is working up to now on testing.
Code:
if ((outputBmi < 18.5) && ((aGender.equals("F" )) || ((aGender.equals("f"))) && ((age <= 19)) )) {
txtResults.setText(" Underweight");
txtResults.setForeground(Color.ORANGE);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi <= 23) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
txtResults.setText(" OK");
txtResults.setForeground(Color.GREEN);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi > 23) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 19)) )) {
txtResults.setText(" Overweight");
txtResults.setForeground(Color.RED);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi < 19.1) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 29)) )) {
txtResults.setText(" Underweight");
txtResults.setForeground(Color.ORANGE); // check bmi readings
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi <= 25.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 29)) )) {
txtResults.setText(" OK");
txtResults.setForeground(Color.GREEN);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi > 25.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 29)) )) {
txtResults.setText(" Overweight");
txtResults.setForeground(Color.RED);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi < 24) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 39)) )) {
txtResults.setText(" Underweight");
txtResults.setForeground(Color.ORANGE);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi <= 25.9) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 39)) )) {
txtResults.setText(" OK");
txtResults.setForeground(Color.GREEN);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi > 25.9) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 39)) )) {
txtResults.setText(" Overweight");
txtResults.setForeground(Color.RED);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi < 19.1) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 49)) )) {
txtResults.setText(" Underweight");
txtResults.setForeground(Color.ORANGE);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi <= 26.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 49)) )) {
txtResults.setText(" OK");
txtResults.setForeground(Color.GREEN);
txtResults.setBackground(Color.BLACK);
} else {
if ((outputBmi > 26.8) && ((aGender.equals("F")) || ((aGender.equals("f"))) && ((age <= 49)) )) {
txtResults.setText(" Overweight");
txtResults.setForeground(Color.RED);
txtResults.setBackground(Color.BLACK);
}
}
Thanks again, always pays to have another set of eyes
lol
:D