简体   繁体   English

为什么不打印我的if and else语句?

[英]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.

相关问题 无法弄清楚为什么 java 在 if else 语句之后忽略了我的打印语句 - Can;t figure out why java is ignoring my print statement after the if else statement 为什么我的数组不打印我的输入? - Why doesn't my array print out my input? 为什么我的代码没有正确打印出课程 - Why doesn't my code print out the Course(s) correctly 为什么我的java代码不打印出数组列表? - Why doesn't my java code print out the array list? 为什么我的 ArrayList 没有打印出 Java 中所需的输出? - Why doesn't my ArrayList print out the output desired in Java? 当我只输入2个输入时,为什么else语句不起作用? - Why doesn't my else statement work when I put only 2 inputs? 当两者的输出相同时,为什么我的if / else语句不匹配? - why doesn't my if/else statement match when the output of both is the same? 为什么我的 main 中的打印输出语句不显示在我的 fillArray 方法中输入的值? - Why isn't the print out statement in my main displaying the values that were entered in my fillArray method? 为什么我的异常不会打印出它的打印语句? (爪哇) - Why will my exception not print out its print statement? (Java) 无法弄清楚为什么我在我的数组print语句中获取空值 - Can't figure out why im getting null values in my array print statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM