Why does my boolean test in java always fail?

戏子无情 提交于 2019-12-31 03:39:04

问题


I am trying to make a boolean test so that if one of the tire pressures is below 35 or over 45 the system outputs "bad inflation".

In my class I must use a boolean, which is what I tried. However the boolean returned is always true. I don't understand why.

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

回答1:


    if (goodPressure = true)

Change this to:

    if (goodPressure == true)

Or even better yet:

    if (goodPressure)

Boolean comparison operators are == and !=. The = is an assignment operator.

Also, you need to initially set goodPressure = true; before you check for violating conditions.




回答2:


you are initializing goodPressure to false, but then never assigning true, so it will always be false. Try initializing it to true.




回答3:


It looks like you're never setting goodPressure to true. Maybe you want to start with it set to true, as it looks like your conditions will set it to false if necessary.

Also, I think this line should throw a compiler warning (or error?)

if (goodPressure = true)

when compiling in Java. I thought the compiler wouldn't let you do an assignment in an if check, but maybe it does... I think you want it to be:

if (goodPressure == true)

Or just:

if (goodPressure)



回答4:


Your problem is that there is only a single = sign in the expression if (goodPressure = true). That assigns true to goodPressure and then checks to see if goodPressure is still true.

You need to use a == or a .equals()




回答5:


Look at the last if statement. You are doing assignment not comparison.

BTW. Your program will always return false once you do that... look at your logic. Where do you set goodPressure to true?




回答6:


Usually, code like if (variable = constantValue) is treated as compilation error in Java. However, there is an exception when the constant value is a boolean. In that case, the statement is equal to if (constantValue). This kind of issue can not be found in the compilation phase.

So, I suggest 1)do not compare with boolean constant value, just do it by if (booleanVar); 2)always put the constant value ahead, like 'if (true = variable)' will cause the compilation fail.



来源:https://stackoverflow.com/questions/2429843/why-does-my-boolean-test-in-java-always-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!