java - Need advice with nested if's -
i have attempted using nested if in following code. have initialized variables compiler telling me variable named 'bill' not initialized though has been. why compiler not recognizing value assigned variable? please see notes in code below.
package killme; import java.util.scanner; public class kill_me { static scanner console = new scanner(system.in); static double premium_service = 55.00; static double premium_day_overtime_min = 0.20; static double premium_night_overtime_min = 0.15; static double regular_service = 30.00; static double regular_overtime_min = 0.40; public static void main(string[] args) { int acctnumber; double premiumdaymin; double premiumnightmin; double bill; double minutes; string name; string premium = "premium"; string regular = "regular"; system.out.println("what account number? "); acctnumber = console.nextint(); system.out.println("what customer name? "); name = console.next(); system.out.println("is service code premium or regular? "); string strservice = console.next(); string strservicecap = strservice.touppercase(); if(strservicecap.compareto(premium) == 0) { system.out.println("how many day minutes used? "); premiumdaymin = console.nextdouble(); system.out.println("how many night minutes used? "); premiumnightmin = console.nextdouble(); if(premiumdaymin <0 && premiumnightmin <0) { system.out.println("minutes cannot less 0 "); } else if(premiumdaymin <= 75 && premiumnightmin <= 100) { bill = premium_service; } else bill = premium_service + (premiumdaymin - 75) * premium_day_overtime_min + (premiumnightmin - 100) * premium_night_overtime_min; minutes = premiumdaymin + premiumnightmin; system.out.println("customer name: " + name); system.out.println("account number: " + acctnumber); system.out.println("service type: " + strservicecap); system.out.println("minutes premium service used (day): " + premiumdaymin); system.out.println("minutes premium service used (night): " + premiumnightmin); system.out.println("amount due: " + bill); // error here stating, "the local variable 'bill' may not have been initialized". } else if(strservicecap.compareto(regular) == 0) { system.out.println("how many minutes used? "); minutes = console.nextdouble(); bill = regular_service + (minutes - 50) * regular_overtime_min; system.out.println("customer name: " + name); system.out.println("account number: " + acctnumber); system.out.println("service type: " + strservicecap); system.out.println("minutes regular service used: " + minutes); system.out.println("amount due: " + bill); // not receive error message here. } else { system.out.println("invalid service type"); } } // end of main } // end of class
no, bill
has not been initialized in cases.
understand this: java compiler never, ever, evaluate boolean expressions; simplified version:
double bill; if (c1) { bill = v1; } else if (c2) { bill = v2; } // try , use bill here
even if, according logic, boolean expressions c1
, c2
may cover possible cases, compiler cannot ensure case.
this root cause of error, deep if/else, switch, etc statements may nested.
Comments
Post a Comment