if-else语句转到错误的条件语句java?

时间:2014-10-08 01:23:09

标签: java if-statement

我的程序输入似乎是在我的if-else语句中输入错误的else语句。

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,并且使用的总水量少于4,000,000加仑,它将执行当使用的总加仑数超过4,000,000加仑时的代码行。为什么呢?

5 个答案:

答案 0 :(得分:2)

由于您的条件的操作顺序

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

customerCode.equals(“c”)|| customerCode.equals(“C”)&amp;&amp;加仑使用&lt; = 4000000

T || F&amp;&amp; ˚F

这相当于true,所以billingAmount = 1000.00;

但下一个陈述是

customerCode.equals(“c”)|| customerCode.equals(“C”)&amp;&amp;加仑使用&gt; 4000000)

T || F&amp;&amp; ˚F

这也等同于true,因此billingAmount被覆盖 - billingAmount = 1000.00 +((gallonsUsed-4000000)* 0.00025);

这两个if语句都符合你的条件。

要修复,请使用括号。并使用else语句。当第一个条件中的一个为真时,没有理由进行多次条件检查。

答案 1 :(得分:0)

因为or-clause ||把()放在它周围。

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

答案 2 :(得分:0)

&amp;&amp;在第二次和第三次测试之间首先评估,然后是||第一次测试(当你输入&#39; 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);
    }
}

答案 3 :(得分:0)

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

相当于

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

因为运算符优先级。具体来说,&&的优先级高于||,因此如果同时使用同一个表达式,它会将&&左侧和右侧的表达式视为{{1}的操作数} {}和&&的结果作为&&的操作数。因此,如果客户代码为小写||,则不会查看c

gallonsUsed部分需要括号:

||

或者使用 if((customerCode.equals("c")||customerCode.equals("C")) && gallonsUsed > 4000000) 并避免整个运算符优先级问题:

equalsIgnoreCase

答案 4 :(得分:0)

然后有更高的先例,或

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“i”,可能看起来像,

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