我的程序输入似乎正在旅行到我的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加仑线。为什么?
是所有代码有关?你为什么不把你的代码片断只减少到相关的部分?你有没有调试过你的应用程序? – 2014-10-08 01:25:48
它符合条件(s)。附加一个调试器并执行代码。 – user2864740 2014-10-08 01:29:53
这是操作顺序的重要性的一个很好的例子,如果...其他如果...语句 – airtech 2014-10-08 01:33:55