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

问题描述:

我想做一个布尔测试,以便如果其中一个轮胎压力低于35或超过45,系统会输出“不良通货膨胀”。为什么我在java中的布尔测试总是失败?

在我的课程中,我必须使用布尔值,这正是我所尝试的。然而返回的布尔值总是为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 
+1

您的代码非常清晰,您不需要评论就可以重复相同的信息(这是件好事!)。为了将来的参考,最重要的一点是要解释为什么你在做什么,而不是你在做什么。 :D – 2010-03-12 01:26:36

+0

您可能还想看看'java.util.Scanner'。 – polygenelubricants 2010-03-12 01:41:50

if (goodPressure = true) 

更改为:

if (goodPressure == true) 

甚至更​​好的是:

if (goodPressure) 

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

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

+1

您还需要将goodPressure初始化为true,否则将始终为false。 – 2010-03-12 01:14:56

+0

谢谢,先生!我知道这很简单! – Cheesegraterr 2010-03-12 01:15:26

+2

如果你发现自己编写了这种类型的bug,你可以通过将常量放在'=='左边(而不是变量)来让编译器抓住它。例如,'if(true = goodPressure)'生成编译器错误。 – Seth 2010-03-12 01:17:05

您正在将goodPressure初始化为false,但从未分配true,所以它始终是false。尝试将其初始化为true。

+0

+1尼斯抓住! – polygenelubricants 2010-03-12 01:18:59

它看起来像你永远不会把goodPressure设置为true。也许你想从它开始设置为true,因为它看起来像你的条件会在必要时将其设置为false。

另外,我觉得这条线应该抛出一个编译器警告(或错误?)

if (goodPressure = true) 

在Java的编译时。我以为,编译器不会让若检查你在一个任务,但也许它......我想你希望它是:

if (goodPressure == true) 

或者只是:

if (goodPressure) 

你问题是表达式if (goodPressure = true)中只有一个=符号。这将真实地分配给良好的压力,然后检查是否良好的压力仍然是正确的。

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

查看最后一条if语句。你在做任务而不是比较。

顺便说一句。一旦你这样做了,你的程序总是会返回假的......看看你的逻辑。你在哪里设定良好压力为真?

通常,像if (variable = constantValue)这样的代码在Java中被视为编译错误。但是,常数值是布尔值时有一个例外。在这种情况下,该声明等于if (constantValue)。编译阶段无法找到这类问题。因此,我建议1)不要与布尔常量值进行比较,只需通过if (booleanVar)来完成; 2)总是把常量值放在前面,比如'if(true = variable)'会导致编译失败。