Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this switch statement: private int actualDecade( int decade) { switch (de

ID: 3649742 • Letter: I

Question

I have this switch statement:

private int actualDecade(int decade)

{

switch (decade)

{

case 0: return 1900;

case 1: return 1910;

case 2: return 1920;

case 3: return 1930;

case 4: return 1940;

case 5: return 1950;

case 6: return 1960;

case 7: return 1970;

case 8: return 1980;

case 9: return 1990;

case 10: return 2000;

}

}


Java keeps telling me :

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

This method must return a result of type int


What can i do to fix this? Is it because there has to be a return statement outside of the switch loop? Is there another way I can implement this other than if else statements?

Explanation / Answer

1.there should be a default case otherwise the compiler won't consider other return statements as actual return statements. it considers the default case return statement as return statement. 2. break each case by appending break; to your code in each case.