如果else语句指向错误的条件语句java?

如果else语句指向错误的条件语句java?,java,if-statement,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("$#,

我对程序的输入似乎转到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.");



   }
}
import java.util.Scanner;
导入java.text.*;
公共类CSCD210Lab6
{
公共静态void main(字符串[]args)
{
扫描仪水输入=新扫描仪(System.in);
DecimalFormat df=新的DecimalFormat($#,###.00”);
DecimalFormat zf=新的DecimalFormat(“0”);
//声明变量
int开始米,结束米;
字符串自定义代码;
双billingAmount,Gallunsed;
billingAmount=0;
System.out.print(“请输入您的客户代码:”);
customerCode=waterInput.next();
System.out.print(“请输入您的起始电表读数:”);
beginMeter=waterInput.nextInt();
如果(起始米<0)
{
System.out.println();
System.out.print(“错误!您输入了负数。程序现在将关闭。”);
系统出口(0);
}
System.out.print(“请输入您的最终仪表读数:”);
endMeter=waterInput.nextInt();
if(端计<0)
{
System.out.println();
System.out.print(“错误!您输入了负数。程序现在将关闭。”);
系统出口(0);
}
如果(endMeter>beginMeter)
{
加仑用量=((双)端米-始米)/10;
}
其他的
{
加仑消耗=(100000000-((双)起始米-终止米))/10;
}
if(customerCode.equals(“r”)| | customerCode.equals(“r”))
{
计费量=5.00+(.0005*加仑);
} 
if(客户代码等于(“c”)| |客户代码等于(“c”)&&GALLUSED 4000000)
{
billingAmount=1000.00+((加仑-4000000)*0.00025);
}
if(客户代码等于(“i”)| |客户代码等于(“i”)&&gallonsUsed 4000000和&gallonsUsed<10000000)
{
billingAmount=2000.00;
}
if(客户代码等于(“i”)| |客户代码等于(“i”)&&GALLUSSED>=10000000)
{
billingAmount=2000.00+(加仑消耗量*.00025);
}
System.out.println();
System.out.print(“您的客户代码为:“+customerCode”);
System.out.println();
System.out.print(“您的起始仪表读数为:“+beginMeter”);
System.out.println();
System.out.print(“您的最终仪表读数为:“+endMeter”);
System.out.println();
System.out.print(“您在此计费期间使用了”+zf.format(加仑数)+“加仑数”);
System.out.println();
系统输出打印(“您的账单是:“+df.format(billingAmount));
System.out.println();
System.out.println();
系统输出打印(“请及时付款。我们讨厌逾期付款。”);
}
}

例如,如果我输入c,总用水量小于4000000加仑,则当总用水量大于4000000加仑时,它将执行代码行。为什么?

这是因为or子句| | put()围绕着它

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

因为你的条件句的操作顺序

客户代码等于(c)| |客户代码等于(c)&&GALLUSED 4000000)

T | | F&&F

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

这两个if语句对于您的情况都是正确的


要修复,请使用括号。也可以使用else语句。当第一个条件之一为真时,没有理由进行大量的条件检查

首先对第二个和第三个测试之间的&&进行求值,然后对第一个测试进行| |求值(输入“c”时为真)

使用括号向读者(和编译器)指示应首先计算if语句的哪些部分。在您的情况下,您的if语句应为:

if((customerCode.equals("c")||customerCode.equals("C")) && gallonsUsed <= 4000000)
if((customerCode.equals(“c”)| | customerCode.equals(“c”)&&gallonsUsed
相当于

  if(customerCode.equals("c") ||
       (customerCode.equals("C") && gallonsUsed > 4000000))
由于运算符的优先级。具体来说,
&
的优先级高于
|
,因此,如果在同一表达式中有这两个表达式,它会将
&&
左侧和右侧的表达式视为
&
的操作数,
&
的结果视为
|
的操作数。因此,如果客户ode是小写的
c
,它不会看到
gallonsUsed

|
部分周围需要括号:

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

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

并且有更高的进阶或者

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") && gallonsUsed > 4000000)
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);
  }
}
if (customerCode.equalsIgnoreCase("i")) {
    if (gallonsUsed <= 4000000) {
        billingAmount = 1000.00;
    } else {
        billingAmount = 2000.00 + ((gallonsUsed < 10000000) ? 0
                : (gallonsUsed * 0.00025));
    }
}