这是一个编程题模板。
编写程序,输人月份和年份,然后显示这个月的天数。例如:如果用户输人的月份是2而年份是2012,那么程序应该显示“February 2012 has 29 days”。如果用户输人的月份为3而年份为2015,那么程序就应该显示“March 2015 has 31 days”)。
输入格式:
输入在一行中,包括月份和年份,用空格隔开。第一个输入的数字范围为1-12,代表1-12月。
输出格式:
输出月份对应的英文,以及年份。格式如:February 2012 has 29 days
输入样例:
在这里给出一组输入。例如:
2 2012
输出样例:
在这里给出相应的输出。例如:
February 2012 has 29 days
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
import java.util.Scanner;
public class Main{
public static void main(String arg[]){
Scanner input = new Scanner(System.in);
int month = input.nextInt();
int year = input.nextInt();
int day=0;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:day=31;
break;
case 4:
case 6:
case 9:
case 11:day=30;
break;
case 2:
if((year%4==0)&&(year%100!=0)||(year%400==0)){
day=29;
}
else{
day=28;
}
break;
}
switch(month){
case 1:System.out.print("January");break;
case 2:System.out.print("February");break;
case 3:System.out.print("March");break;
case 5:System.out.print("May");break;
case 7:System.out.print("July");break;
case 8:System.out.print("August");break;
case 10:System.out.print("October");break;
case 12:System.out.print("December");break;
case 4:System.out.print("April");break;
case 6:System.out.print("June");break;
case 9:System.out.print("September");break;
case 11:System.out.print("November");break;
}
System.out.println(" "+year+" has "+day+" days");
}
}