this question has answer here:
i given assignment homework:
create program in java accepts unsigned integer n , return number of days according month. example if n = 6, return value 30 because 6th month of june has 30 days. assume no leap year.
this attempt, doesn't work expected. can give me pointers why.
public class daysmonths { public static void main(string []args) { (int = 1; i<=12; i++){ int e = f(i); system.out.println(i + " = " + e + " days "); } } public static int f(int i){ if ((i == 1)|(i == 3)|(i == 5)|(i == 7)|(i == 8)|(i == 10)|(i == 12)) return 31; else if ((i == 4)|(i == 6)|(i == 9)|(i == 11)) return 30; else return 28; } }
the below code uses java calendar class setting it's month input month , getting max date getactualmaximum() method. return 29 leap years.
public static void main(string args[]){ int month = 6; calendar cal = calendar.getinstance(); cal.set(calendar.month, month-1); system.out.println(cal.getactualmaximum(calendar.date)); }
Comments
Post a Comment