为什么我在java中的布尔测试总是失败?

时间:2010-03-12 01:07:38

标签: java boolean equals equality equals-operator

我正在尝试进行布尔测试,以便在其中一个轮胎压力低于35或超过45时系统输出“不良通货膨胀”。

在我班上我必须使用布尔值,这是我尝试过的。但是返回的布尔值始终为true。我不明白为什么。

public class tirePressure
{
    private static double getDoubleSystem1 ()  //Private routine to simply read a double in from the command line
    {
        String myInput1 = null; //Store the string that is read form the command line
        double numInput1 = 0;      //Used to store the converted string into an double
        BufferedReader mySystem; //Buffer to store input
        mySystem = new BufferedReader (new InputStreamReader (System.in)); // creates a connection to system files or cmd
        try
        {
            myInput1 = mySystem.readLine (); //reads in data from console
            myInput1 = myInput1.trim (); //trim command cuts off unneccesary inputs
        }
        catch (IOException e)  //checks for errors
        {
            System.out.println ("IOException: " + e);
            return -1;
        }

        numInput1 = Double.parseDouble (myInput1); //converts the string to an double
        return numInput1;                       //return double value to main program
    }

    static public void main (String[] args)
    {
        double TireFR; //double to store input from console
        double TireFL;
        double TireBR;
        double TireBL;
        boolean goodPressure;
        goodPressure = false;

        System.out.println ("Tire Pressure Checker");
        System.out.println (" ");

        System.out.print ("Enter pressure of front left tire:");
        TireFL = getDoubleSystem1 ();    //read in an double from the user

        if (TireFL < 35 || TireFL > 45)
        {
            System.out.println ("Pressure out of range");
            goodPressure = false;
        }

        System.out.print ("Enter pressure of front right tire:");
        TireFR = getDoubleSystem1 ();    //read in an double from the user

        if (TireFR < 35 || TireFR > 45)
        {
            System.out.println ("Pressure out of range");
            goodPressure = false;

        }

        if (TireFL == TireFR)
            System.out.print (" ");
        else
            System.out.println ("Front tire pressures do not match");
        System.out.println (" ");

        System.out.print ("Enter pressure of back left tire:");
        TireBL = getDoubleSystem1 ();    //read in an double from the user

        if (TireBL < 35 || TireBL > 45)
        {
            System.out.println ("Pressure out of range");
            goodPressure = false;
        }

        System.out.print ("Enter pressure of back right tire:");
        TireBR = getDoubleSystem1 ();    //read in an double from the user

        if (TireBR < 35 || TireBR > 45)
        {
            System.out.println ("Pressure out of range");
            goodPressure = false;
        }

        if (TireBL == TireBR)
            System.out.print (" ");
        else
            System.out.println ("Back tire pressures do not match");

        if (goodPressure = true)
            System.out.println ("Inflation is OK.");
        else
            System.out.println ("Inflation is BAD.");

        System.out.println (goodPressure);


    } //mainmethod
} // tirePressure Class

6 个答案:

答案 0 :(得分:15)

    if (goodPressure = true)

将其更改为:

    if (goodPressure == true)

甚至更好:

    if (goodPressure)

布尔比较运算符为==!==是一个赋值运算符。

此外,在检查违规情况之前,您需要先设置goodPressure = true;

答案 1 :(得分:1)

您正在将goodPressure初始化为false,但之后从未指定true,因此它始终为false。尝试将其初始化为true。

答案 2 :(得分:0)

看起来你从来没有将goodPressure设置为true。也许你想从设置为true开始,因为看起来你的条件会在必要时将其设置为false。

另外,我认为这一行应该抛出编译器警告(或错误?)

if (goodPressure = true)

在Java中编译时。我认为编译器不允许你在if检查中进行任务,但也许它确实...我认为你希望它是:

if (goodPressure == true)

或者只是:

if (goodPressure)

答案 3 :(得分:0)

您的问题是表达式if (goodPressure = true)中只有一个=符号。这将为goodPressure指定true,然后检查goodPressure是否仍然是真的。

您需要使用==或.equals()

答案 4 :(得分:0)

查看最后的if语句。你做的任务不是比较。

顺便说一句。一旦你这样做,你的程序将总是返回false ...看看你的逻辑。你在哪里设置goodPressure为真?

答案 5 :(得分:0)

通常,像if (variable = constantValue)这样的代码在Java中被视为编译错误。但是,常量值是布尔值时会有异常。在这种情况下,该陈述等于if (constantValue)。在编译阶段无法找到此类问题。

所以,我建议1)不要与布尔常量值进行比较,只需按if (booleanVar)进行; 2)总是把常量值放在前面,比如'if(true = variable)'会导致编译失败。