简体   繁体   English

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

[英]Why does my boolean test in java always fail?

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". 我正在尝试进行布尔测试,以便如果其中一个轮胎压力低于35或超过45,则系统输出“不良通货膨胀”。

In my class I must use a boolean, which is what I tried. 在我的课堂上,我必须使用布尔值,这是我尝试过的。 However the boolean returned is always true. 但是返回的布尔值始终为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
    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; 此外,您需要初始设置goodPressure = true; before you check for violating conditions. 在检查违规情况之前。

you are initializing goodPressure to false, but then never assigning true, so it will always be false. 你正在将goodPressure初始化为false,但是从不指定true,所以它总是假的。 Try initializing it to true. 尝试将其初始化为true。

It looks like you're never setting goodPressure to true. 看起来你永远都不会将goodPressure设置为true。 Maybe you want to start with it set to true, as it looks like your conditions will set it to false if necessary. 也许你想从设置为true开始,因为看起来你的条件会在必要时将其设置为false。

Also, I think this line should throw a compiler warning (or error?) 另外,我认为这行应该抛出编译器警告(或错误?)

if (goodPressure = true)

when compiling in Java. 在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检查中进行任务,但也许它确实...我认为你希望它是:

if (goodPressure == true)

Or just: 要不就:

if (goodPressure)

Your problem is that there is only a single = sign in the expression if (goodPressure = true) . 你的问题是表达式中只有一个=符号if (goodPressure = true) That assigns true to goodPressure and then checks to see if goodPressure is still true. 这将为goodPressure指定true,然后检查goodPressure是否仍为true。

You need to use a == or a .equals() 你需要使用==或.equals()

Look at the last if statement. 看看最后的if语句。 You are doing assignment not comparison. 你做的任务不是比较。

BTW. BTW。 Your program will always return false once you do that... look at your logic. 一旦你这样做,你的程序将总是返回false ...看看你的逻辑。 Where do you set goodPressure to true? 你在哪里设置goodPressure为真?

Usually, code like if (variable = constantValue) is treated as compilation error in Java. 通常,像if (variable = constantValue)这样的代码在Java中被视为编译错误。 However, there is an exception when the constant value is a boolean. 但是,常量值是布尔值时会有异常。 In that case, the statement is equal to if (constantValue) . 在这种情况下,语句等于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) ; 所以,我建议1)不要与布尔常量值进行比较,只需通过if (booleanVar) ; 2)always put the constant value ahead, like 'if (true = variable)' will cause the compilation fail. 2)总是把常量值放在前面,比如'if(true = variable)'会导致编译失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM