2010-03-12 157 views
2

我想做一個布爾測試,以便如果其中一個輪胎壓力低於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

回答

15
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

1

您正在將goodPressure初始化爲false,但從未分配true,所以它始終是false。嘗試將其初始化爲true。

+0

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

0

它看起來像你永遠不會把goodPressure設置爲true。也許你想從它開始設置爲true,因爲它看起來像你的條件會在必要時將其設置爲false。

另外,我覺得這條線應該拋出一個編譯器警告(或錯誤?)

if (goodPressure = true) 

在Java的編譯時。我以爲,編譯器不會讓若檢查你在一個任務,但也許它......我想你希望它是:

if (goodPressure == true) 

或者只是:

if (goodPressure) 
0

你問題是表達式if (goodPressure = true)中只有一個=符號。這將真實地分配給良好的壓力,然後檢查是否良好的壓力仍然是正確的。

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

0

查看最後一條if語句。你在做任務而不是比較。

順便說一句。一旦你這樣做了,你的程序總是會返回假的......看看你的邏輯。你在哪裏設定良好壓力爲真?

0

通常,像if (variable = constantValue)這樣的代碼在Java中被視爲編譯錯誤。但是,常數值是布爾值時有一個例外。在這種情況下,該聲明等於if (constantValue)。編譯階段無法找到這類問題。因此,我建議1)不要與布爾常量值進行比較,只需通過if (booleanVar)來完成; 2)總是把常量值放在前面,比如'if(true = variable)'會導致編譯失敗。

相關問題