java - Error: variable digitMonth might not have been initialized -
i'm trying write program takes birth year, month, , day , calculates day said birthday lies on. thing is, error message:
error: variable digitmonth might not have been initialized total = inputyear + test2 + digitmonth + inputday; ^
i don't know how fix it. setting digitmonth number (e.g. 1)makes program work, formula, each month requires different number (1 january, 4, february, 4 march, 0 april, etc)
i've looked @ other questions errors , haven't found useful yet.
help?
import java.util.scanner; public class birth{ public static void main(string[] args) { scanner scan = new scanner(system.in); int inputyear, inputmonth, inputday, digitmonth, test2, total, daynum; system.out.println("enter last 2 digits of year born in"); inputyear = scan.nextint(); system.out.println("enter month number born in"); inputmonth = scan.nextint(); system.out.println("enter birth day"); inputday = scan.nextint(); test2 = inputyear / 4; if (inputmonth > 0 && inputmonth < 2) { digitmonth = 1; } else if (inputmonth > 1 && inputmonth < 3) { digitmonth = 4; } else if (inputmonth > 2 && inputmonth < 4) { digitmonth = 4; } else if (inputmonth > 3 && inputmonth < 5) { digitmonth = 0; } else if (inputmonth > 4 && inputmonth < 6) { digitmonth = 2; } else if (inputmonth > 5 && inputmonth < 7) { digitmonth = 5; } else if (inputmonth > 6 && inputmonth < 8) { digitmonth = 0; } else if (inputmonth > 7 && inputmonth < 9) { digitmonth = 3; } else if (inputmonth > 8 && inputmonth < 10) { digitmonth = 6; } else if (inputmonth > 9 && inputmonth < 11) { digitmonth = 1; } else if (inputmonth > 10 && inputmonth < 12) { digitmonth = 4; } else if (inputmonth > 11 && inputmonth < 13) { digitmonth = 6; } else system.out.println("you fuck-up"); total = inputyear + test2 + digitmonth + inputday; daynum = total / 7; if (daynum > 0 || daynum < 2) { system.out.println("you born on sunday"); } else if (daynum > 1 || daynum < 3) { system.out.println("you born on monday"); } else if (daynum > 2 || daynum < 4) { system.out.println("you born on tuesday"); } else if (daynum > 3 || daynum < 5) { system.out.println("you born on wednesday"); } else if (daynum > 4 || daynum < 6) { system.out.println("you born on thursday"); } else if (daynum > 5 || daynum < 7) { system.out.println("you born on friday"); } else if (daynum > -1 || daynum < 1) { system.out.println("you born on saturday"); } }
}
you assign value digitmonth
inside if
, else if
statements , not on final else
statement.
happenes if none of if
, else if
succeeds , else
part does?
value of digitmonth
?
answer: none
may never happen in code nevertheless compiler complains it.
there 2 solutions:
1) give initial value digitmonth
. i.e. int digitmonth = 0;
2) initialize digitmonth
inside final else. i.e:
else{ //some code digitmonth = 0; }
hope helps
Comments
Post a Comment