2014-10-08 74 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