2014-10-08 75 views
0

我的程序輸入似乎正在旅行到我的if-else語句中的錯誤else語句。If-else語句去了錯誤的條件語句java?

import java.util.Scanner; 
import java.text.*; 

public class CSCD210Lab6 
{ 
    public static void main (String [] args) 
    { 
    Scanner waterInput = new Scanner(System.in); 
    DecimalFormat df = new DecimalFormat("$#,###.00"); 
    DecimalFormat zf = new DecimalFormat("#,###.0"); 

     //declare variables 
     int beginMeter, endMeter; 
     String customerCode; 
     double billingAmount,gallonsUsed; 


     billingAmount = 0; 

     System.out.print("Please Enter Your Customer Code: "); 
     customerCode = waterInput.next(); 
     System.out.print("Please Enter Your Beginning Meter Reading: "); 
     beginMeter = waterInput.nextInt(); 
     if(beginMeter < 0) 
     { 
      System.out.println(); 
      System.out.print("ERROR! You Have Entered A Negative Number. The Program Will Now Close."); 
      System.exit(0); 
     } 

     System.out.print("Please Enter Your Ending Meter Reading: "); 
     endMeter = waterInput.nextInt(); 
     if(endMeter < 0) 
     { 
      System.out.println(); 
      System.out.print("ERROR! You Have Entered A Negative Number. The Program Will Now Close."); 
      System.exit(0); 
     } 
     if(endMeter > beginMeter) 
     { 
     gallonsUsed = ((double)endMeter - beginMeter)/10; 
     } 
     else 
     { 
     gallonsUsed = (1000000000-((double)beginMeter - endMeter))/10; 
     } 
     if (customerCode.equals("r")||customerCode.equals("R")) 
     { 
     billingAmount = 5.00 + (.0005 * gallonsUsed); 
     } 

     if(customerCode.equals("c")||customerCode.equals("C") && gallonsUsed <= 4000000)  
     { 
     billingAmount = 1000.00; 
     } 

     if(customerCode.equals("c")||customerCode.equals("C") && gallonsUsed > 4000000) 
     { 
     billingAmount = 1000.00 + ((gallonsUsed-4000000) * 0.00025); 
     } 

     if(customerCode.equals("i")||customerCode.equals("I")&& gallonsUsed <= 4000000) 
     { 
     billingAmount = 1000.00; 
     } 

     if(customerCode.equals("i")||customerCode.equals("I")&& gallonsUsed > 4000000 && gallonsUsed < 10000000) 
     { 
     billingAmount = 2000.00; 
     } 
     if(customerCode.equals("i")||customerCode.equals("I")&& gallonsUsed >= 10000000) 
     { 
     billingAmount = 2000.00 +(gallonsUsed * .00025); 
     } 


     System.out.println(); 
     System.out.print("Your Customer Code is: "+customerCode); 
     System.out.println(); 
     System.out.print("Your Beginning Meter Reading is: "+beginMeter); 
     System.out.println(); 
     System.out.print("Your Ending Meter Reading is: "+endMeter); 
     System.out.println(); 
     System.out.print("You Have Used "+zf.format(gallonsUsed)+" Gallons During This Billing Period."); 
     System.out.println(); 
     System.out.print("Your Bill is: "+df.format(billingAmount)); 
     System.out.println(); 
     System.out.println(); 
     System.out.print("Please Pay Promptly. We Detest Late Accounts."); 



    } 
} 

例如,如果我輸入c,並在不到4000000加侖所使用的總的水,它的執行代碼時所使用的總加侖多於4000000加侖線。爲什麼?

+0

是所有代碼有關?你爲什麼不把你的代碼片斷只減少到相關的部分?你有沒有調試過你的應用程序? – 2014-10-08 01:25:48

+0

它符合條件(s)。附加一個調試器並執行代碼。 – user2864740 2014-10-08 01:29:53

+0

這是操作順序的重要性的一個很好的例子,如果...其他如果...語句 – airtech 2014-10-08 01:33:55

回答

0

它,因爲或子句||放在()周圍。

if((customerCode.equals("c")||customerCode.equals("C")) && gallonsUsed > 4000000) 
2

由於操作的順序與您的條件句

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

customerCode.equals( 「C」)|| customerCode.equals( 「C」)& & gallonsUsed < = 4000000

Ť|| F & & F

這相當於true,所以billingAmount = 1000.00;

但很下一個語句是

customerCode.equals( 「C」)|| customerCode.equals( 「C」)& & gallonsUsed> 4000000)

Ť|| F & & F

這也等同於true,因此billingAmount被覆蓋 - billingAmount = 1000.00 +((gallonsUsed-4000000)* 0.00025);

這兩個if語句是你的條件真的。

要修復,使用括號。並且還使用else語句。當第一個條件爲真時,沒有理由通過大量的條件檢查。

0

的& &在第二和第三測試之間得到第一評價,然後||與第一次測試(當你輸入'c'時是這樣的)。

使用括號,來表示if語句的部分應首先評估的讀者(和編譯器)。在你的情況,你的if語句應該是:

if((customerCode.equals("c")||customerCode.equals("C")) && gallonsUsed <= 4000000) 

一個更好的方法是使用下面的模式:

if(customerCode.equals("c")||customerCode.equals("C"))  
{ 
    if(gallonsUsed <= 4000000) 
    { 
     billingAmount = 1000.00; 
    } else { 
     billingAmount = 1000.00 + ((gallonsUsed-4000000) * 0.00025); 
    } 
} 
0
if(customerCode.equals("c")||customerCode.equals("C") && gallonsUsed > 4000000) 

相當於

if(customerCode.equals("c") || 
     (customerCode.equals("C") && gallonsUsed > 4000000)) 
因爲

運算符優先級。具體而言,&&||具有更高的優先級,因此如果兩者具有相同的表達式,則它將&&的左側和右側的表達式作爲&&的操作數,並將&&的結果作爲||的操作數。因此,如果客戶代碼爲小寫c,則不會查看gallonsUsed

您需要周圍的||部分括號:

if((customerCode.equals("c")||customerCode.equals("C")) && gallonsUsed > 4000000) 

或者使用equalsIgnoreCase和避免整個運營商的優先問題:

if (customerCode.equalsIgnoreCase("c") && gallonsUsed > 4000000) 
0

,具有較高的preceence然後或者

if(customerCode.equals("c")||customerCode.equals("C") && gallonsUsed <= 4000000)  
{ 
    billingAmount = 1000.00; 
} 
if(customerCode.equals("c")||customerCode.equals("C") && gallonsUsed > 4000000) 
{ 
    billingAmount = 1000.00 + ((gallonsUsed-4000000) * 0.00025); 
} 

所以,你想要

if((customerCode.equals("c")||customerCode.equals("C")) && gallonsUsed <= 4000000)  
{ 
    billingAmount = 1000.00; 
} 
if((customerCode.equals("c")||customerCode.equals("C")) && gallonsUsed > 4000000) 
{ 
    billingAmount = 1000.00 + ((gallonsUsed-4000000) * 0.00025); 
} 

但是我把它改寫爲

if (customerCode.equalsIgnoreCase("c")) { 
    billingAmount = 1000.00 + (gallonsUsed <= 4000000) ? 0 : 
     ((gallonsUsed-4000000) * 0.00025); 
} 

,或者

if (customerCode.equalsIgnoreCase("c")) { 
    if (gallonsUsed <= 4000000) { 
    billingAmount = 1000.00; 
    } else { 
    billingAmount = 1000.00 + ((gallonsUsed-4000000) * 0.00025); 
    } 
} 

而對於customerCode 「我」,可能看起來像,

if (customerCode.equalsIgnoreCase("i")) { 
    if (gallonsUsed <= 4000000) { 
     billingAmount = 1000.00; 
    } else { 
     billingAmount = 2000.00 + ((gallonsUsed < 10000000) ? 0 
       : (gallonsUsed * 0.00025)); 
    } 
} 
+0

你會修復他的其他代碼嗎? :)雖然,我認爲在這種情況下,內聯條件會降低可讀性/可維護性。我會建議編程的可讀性,並讓編譯器進行內聯優化。 – airtech 2014-10-08 01:35:18