下面是一个简单的实现代码:public class java { public static void main(String[] args) throws IOException { BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));System.out.println("请输入年份:");String s1=buf.readLine();System.out.println("请输入月份: ");String...
输入年份月份,打印输出当月日历 用java
编写一个Java程序,让用户输入年份和月份,并输出该月份的日历。首先,我们需要导入必要的包,例如java.io.BufferedReader和java.io.InputStreamReader。
下面是一个简单的实现代码:
public class java {
public static void main(String[] args) throws IOException {
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入年份:");
String s1=buf.readLine();
System.out.println("请输入月份: ");
String s2=buf.readLine();
int year=Integer.parseInt(s1);
int month=Integer.parseInt(s2);
switch(month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 13:
System.out.println("在"+year+"年的"+month+"月有:31天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("在"+year+"年的"+month+"月有:30天");
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0) {
System.out.println("在"+year+"年的"+month+"月有:29天");
} else {
System.out.println("在"+year+"年的"+month+"月有:28天");
}
break;
}
}
}
}
此代码实现了用户输入年份和月份后,根据输入的月份判断该月份有多少天。对于二月份,还需判断是否为闰年,以决定是否为29天。
通过这种方式,我们可以简单地实现一个日历打印功能。2024-12-17