Java: inner test class compilation error - printing all cases of switch block -
edit: had been syntax issue - had been complete noob @ time of posting this, , had not been using proper ide, apologies community. also, please try avoid assuming on site guy.
i'm wrestling inner class datetest
. error message appears after compiling, , haven't been able figure out how fix bug. i've gone through many lines of code work out test class in trying effort print out twelve cases of switch block in date class.
below i've included errors , code. might worth mentioning i'm writing in online ide, browxy, , double checking in codechef.
this code:
public class date { public static int daysinmonth(int month) { /** method uses switch block , takes in month number (1 january, 2 february, etc.) , returns number of days in month. **/ switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; break; case 2: return 28; break; case 4: case 6: case 9: case 11: return 30; break; default: system.out.println("invalid month."); break; } return month; } public class datetest { /*test class prints out 12 lines like: "january 31 days long.", "february...", etc.*/ int month; /* <-- compilation error had been here due missing curly brace (error = "class expected") */ } public static void main (string [] args) { month = date.daysinmonth(1); system.out.println("january " +month+ " days long."); month = date.daysinmonth(2); system.out.println("february " +month+ " days long."); month = date.daysinmonth(3); system.out.println("march " +month+ " days long."); month = date.daysinmonth(4); system.out.println("april " +month+ " days long."); month = date.daysinmonth(5); system.out.println("may " +month+ " days long."); month = date.daysinmonth(6); system.out.println("june " +month+ " days long."); month = date.daysinmonth(7); system.out.println("july " +month+ " days long."); month = date.daysinmonth(8); system.out.println("august " +month+ " days long."); month = date.daysinmonth(9); system.out.println("september " +month+ " days long."); month = date.daysinmonth(10); system.out.println("october " +month+ " days long."); month = date.daysinmonth(11); system.out.println("november " +month+ " days long."); month = date.daysinmonth(12); system.out.println("december " +month+ " days long."); } }
(edit: first question had asked here, , had begun learning java & how program on own - advice starting out: use decent enough computer can support basic ide syntax highlighting , not online environment/one without this, since java/c++ punctuation may give nightmares when you're not familiar it.)
month
should local member in main
function. add type declaration first time use it, , should fine:
public class datetest { public static void main (string [] args) { int month = date.daysinmonth(1); // rest of code comes here } }
Comments
Post a Comment