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

Che*_*err 2 java boolean equality equals 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
Run Code Online (Sandbox Code Playgroud)

pol*_*nts 15

    if (goodPressure = true)
Run Code Online (Sandbox Code Playgroud)

将其更改为:

    if (goodPressure == true)
Run Code Online (Sandbox Code Playgroud)

或者甚至更好:

    if (goodPressure)
Run Code Online (Sandbox Code Playgroud)

布尔比较运算符是==!=.这=是一个赋值运算符.

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

  • 如果你发现自己经常编写这种bug,可以让编译器通过在`==`(而不是变量)的左侧放置常量来捕获它.例如,`if(true = goodPressure)`生成编译器错误. (2认同)
  • +或"甚至更好:if(goodPressure)" (2认同)