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

时间:2014-02-19 04:31:44

标签: java

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);
        }

    }
  }
}

为什么不在底部打印出if和else语句?我做错了什么?我正在计算一个人的体脂肪。我被困在这里

3 个答案:

答案 0 :(得分:4)

gender不能同时等同于'm''f'。请改为使用逻辑OR运算符||

while ((gender == 'm') || (gender == 'f'))

答案 1 :(得分:0)

性别不能同时为m和f ..在你的同时使用||

..并将您的while更改为if

if ((gender == 'm') || (gender == 'f'))

如果您不希望程序退出,则需要在更高级别循环。

答案 2 :(得分:0)

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...
    }

}

编辑:

if(gender=='m')
{
//formulae here
}
else {
//female calc formulae here
}

//syso(data);