Why does my boolean test in java always fail?

2020-04-12 15:59发布

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

6条回答
▲ chillily
2楼-- · 2020-04-12 16:42

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

查看更多
淡お忘
3楼-- · 2020-04-12 16:43

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()

查看更多
贼婆χ
4楼-- · 2020-04-12 16:53

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?

查看更多
Melony?
5楼-- · 2020-04-12 17:01

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)
查看更多
唯我独甜
6楼-- · 2020-04-12 17:02

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.

查看更多
姐就是有狂的资本
7楼-- · 2020-04-12 17:04
    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.

查看更多
登录 后发表回答