If-else statements going to the wrong conditional statement java? -
If-else statements going to the wrong conditional statement java? -
my input programme seems traveling wrong else statement in if-else statements.
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 come in client code: "); customercode = waterinput.next(); system.out.print("please come in origin meter reading: "); beginmeter = waterinput.nextint(); if(beginmeter < 0) { system.out.println(); system.out.print("error! have entered negative number. programme close."); system.exit(0); } system.out.print("please come in ending meter reading: "); endmeter = waterinput.nextint(); if(endmeter < 0) { system.out.println(); system.out.print("error! have entered negative number. programme 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 client code is: "+customercode); system.out.println(); system.out.print("your origin 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 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. detest late accounts."); } }
for example, if come in c, , total water used in less 4,000,000 gallons, executes line of code when total gallons used more 4,000,000 gallons. why?
because of order of operations conditionals
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
customercode.equals("c")||customercode.equals("c") && gallonsused <= 4000000
t || f && f
this equates true, billingamount = 1000.00;
but next statement
customercode.equals("c")||customercode.equals("c") && gallonsused > 4000000)
t || f && f
this equates true, billingamount gets overridden - billingamount = 1000.00 + ((gallonsused-4000000) * 0.00025);
both of these if statements true condition.
to fix, utilize parenthesis. , utilize else statements. there no reason go through numerous status checks when 1 of first ones true.
java if-statement
Comments
Post a Comment