如何计算一个月内的天数?用户输入当月号。 我为上面写了一段代码,想知道它是否合适。
public class Exercise{
public static void main(String[] args) {
System.out.print("Input a month number:");
Scanner scan ;
int month = 1; int monthName = 1;
while(month < 12 && month > 0) {
scan = new Scanner(System.in);
monthName = scan.nextInt();
month= monthName-1;
DateFormat df = new SimpleDateFormat("MMM", Locale.US);
Calendar mycal = Calendar.getInstance();
mycal.set(Calendar.YEAR, 2017);
mycal.set(Calendar.MONTH, month);
int numDays = mycal.getActualMaximum(Calendar.DATE);
System.out.println(" The no of days in the month of is "+ " "+ numDays);
}
}
}