gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:23:52 24 4
gpt4 key购买 nike

我的程序输入似乎在 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 加仑时,它会执行该行代码。为什么?

最佳答案

由于条件的运算顺序

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

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

T || F&&F

这等于 true,因此 billingAmount = 1000.00;

但是下一个语句是

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

T || F&&F

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

这两个 if 语句都适合您的情况。

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

关于java - if-else语句去错误的条件语句java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26247819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com