[英]why doesn't it print out my if and else statement?
import java.util.*;
public class Lab4
{
public static void main(String[] args)
{
System.out.println("Body Fat Calculator");
double A1,A2,A3,A4,A5,B; //female
double a1,a2,b; //male
double bodyWeight,wristMeasurement,waistMeasurement,hipMeasurement,forearmMeasurement; //female
double bodyFat,bodyFatpercent; //male
Scanner body = new Scanner (System.in);
System.out.println ("Enter Gender (m/f): ");
char gender = body.nextLine().charAt(0);
while ((gender != 'm') && (gender != 'f')) {
System.out.println ("Unknown gender, Enter gender again (m/f); ");
gender = body.nextLine ().charAt(0);
}
System.out.println ("Enter Body Weight: ");
bodyWeight = body.nextInt ();
System.out.println ("Enter Wrist Measurement: ");
wristMeasurement = body.nextDouble ();
System.out.println ("Enter Waist Measurement: ");
waistMeasurement = body.nextDouble ();
System.out.println ("Enter Hip Measurement: ");
hipMeasurement = body.nextDouble ();
System.out.println ("Forearm Measurement: ");
forearmMeasurement = body.nextDouble ();
A1 = (bodyWeight * 0.732) + 8.987;
A2 = wristMeasurement / 3.14;
A3 = waistMeasurement * 0.157;
A4 = hipMeasurement * 0.249;
A5 = forearmMeasurement * 0.434;
B = A1 + A2 - A3 - A4 + A5;
a1 = (bodyWeight * 1.082) + 94.42;
a2 = waistMeasurement * 4.15;
b = a1 - a2;
bodyFat = bodyWeight - b;
bodyFatpercent = bodyFat * 100 / bodyWeight;
while ((gender == 'm') && (gender == 'f')) {
if (gender == 'm') {
a1 = (bodyWeight * 1.082) + 94.42;
a2 = waistMeasurement * 4.15;
b = a1 - a2;
bodyFat = bodyWeight - b;
System.out.println (bodyFatpercent);
}
else {
A1 = (bodyWeight * 0.732) + 8.987;
A2 = wristMeasurement / 3.14;
A3 = waistMeasurement * 0.157;
A4 = hipMeasurement * 0.249;
A5 = forearmMeasurement * 0.434;
B = A1 + A2 - A3 - A4 + A5;
bodyFat = bodyWeight - b;
System.out.println (bodyFatpercent);
}
}
}
}
Why doesn't it print out my if and else statement in the bottom? 为什么不在底部打印出if and else语句? what am I doing wrong with this? 我在做什么错呢? I'm trying to calculate the body fat of a person. 我正在尝试计算一个人的体内脂肪。 I'm stuck here 我被困在这里
gender
cannot be equal to 'm'
and 'f'
at the same time. gender
不能同时等于'm'
和'f'
。 Use the logical OR operator ||
使用逻辑或运算符||
instead: 代替:
while ((gender == 'm') || (gender == 'f'))
Gender can't be both m and f.. use ||
性别不能同时M和F ..使用||
in your while. 在你的时间。
.. And change your while
to an if
! ..并将您的while
更改为if
!
if ((gender == 'm') || (gender == 'f'))
You'll need to loop at a higher level if you dont want the program to exit. 如果您不希望程序退出,则需要在更高级别循环。
bodyFatpercent = bodyFat * 100 / bodyWeight; // you are not changing the value of bodyFatpercent anywhere below.. you are just printing it in your below declared if-else blocks..
while ((gender == 'm') && (gender == 'f')) {
if (gender == 'm') {
a1 = (bodyWeight * 1.082) + 94.42;
a2 = waistMeasurement * 4.15;
b = a1 - a2;
bodyFat = bodyWeight - b;
System.out.println (bodyFatpercent);// what is the use of lines of code in above declared if block???... you are calculating value of bodyFat and printing value of bodyFatpercent...
}
else {
A1 = (bodyWeight * 0.732) + 8.987;
A2 = wristMeasurement / 3.14;
A3 = waistMeasurement * 0.157;
A4 = hipMeasurement * 0.249;
A5 = forearmMeasurement * 0.434;
B = A1 + A2 - A3 - A4 + A5;
bodyFat = bodyWeight - b;
System.out.println (bodyFatpercent); // what is the use of lines of code in above declared else block???... you are calculating value of bodyFat and printing value of bodyFatpercent...
}
}
EDIT : 编辑:
if(gender=='m')
{
//formulae here
}
else {
//female calc formulae here
}
//syso(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.