我的程序輸入似乎正在旅行到我的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