简体   繁体   English

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

[英]If-else statements going to the wrong conditional statement java?

My input for my program seems to be traveling to the wrong else statement in my if-else statements. 我对程序的输入似乎在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.");



   }
}

For example, if I enter c, and the total water used in less than 4,000,000 gallons, it executes the line of code for when the total gallons used is more than 4,000,000 gallons. 例如,如果我输入c,并且用水总量少于4,000,000加仑,则当所使用的总加仑大于4,000,000加仑时,它将执行代码行。 Why? 为什么?

Because of the order of operations with your conditionals 由于条件操作的顺序

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

customerCode.equals("c")||customerCode.equals("C") && gallonsUsed <= 4000000 customerCode.equals(“ c”)|| customerCode.equals(“ C”)&&加仑已使用<= 4000000

T || T || F && F F && F

This equates to true, so billingAmount = 1000.00; 这等于true,所以billingAmount = 1000.00;

But the very next statement is 但接下来的声明是

customerCode.equals("c")||customerCode.equals("C") && gallonsUsed > 4000000) customerCode.equals(“ c”)|| customerCode.equals(“ C”)&&加仑已使用> 4000000)

T || T || F && F F && F

This also equates to true, so billingAmount gets overridden - billingAmount = 1000.00 + ((gallonsUsed-4000000) * 0.00025); 这也等于true,因此billingAmount被覆盖-billingAmount = 1000.00 +(((gallonsUsed-4000000)* 0.00025);

Both of these if statements are true for your condition. 这两个if语句都符合您的条件。

To fix, use parenthesis. 要解决此问题,请使用括号。 And also use else statements. 并且还可以使用else语句。 There is no reason to go through the numerous condition checks when one of the first ones is true. 当第一个条件为真时,没有理由进行大量条件检查。

Its because the or-clause || 其原因是|| put ( ) around it. 将()放在它周围。

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

The && between the second and third test gets evaluated first, then || 首先评估第二个和第三个测试之间的&&,然后|| with the first test (which is true when you enter 'c'). 第一个测试(输入“ c”时为true)。

Use parentheses to indicate to the reader (and compiler) which parts of the if statement should be evaluated first. 使用括号指示读者(和编译器)首先应评估if语句的哪些部分。 In your case, your if statement should read: 就您而言,您的if语句应为:

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

A better approach would be to use the following pattern: 更好的方法是使用以下模式:

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

is equivalent to 相当于

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

because of operator precedence. 由于运算符的优先级。 Specifically, && has a higher precedence than || 具体而言, &&的优先级高于|| , so if you have both in the same expression, it treats the expressions to the left and right of && as the operands of && , and the result of && as the operand of || ,所以如果你有两个在同一个表达式,它把表达式的左侧和右侧&&作为操作数&&的,结果&&作为操作数|| . So if the customer code is lower-case c , it won't look at gallonsUsed . 因此,如果客户代码为小写c ,则不会查看gallonsUsed

You need parentheses around the || 您需要在||周围加上括号 part: 部分:

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

Or use equalsIgnoreCase and avoid the whole operator-precedence problem: 或使用equalsIgnoreCase并避免整个运算符优先级问题:

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

And has higher preceence then or, 而且比

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);
}

So, you wanted 所以,你想要

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);
}

But I would rewrite it as 但我会将其重写为

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

or, 要么,

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

And for customerCode "i" that might look like, 对于看起来像的customerCode “ i”,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM