实验2:制作某年某月的日历。
实验目的:掌握Calendar类的使用。
实验要求:通过键盘输入年份和月份,程序输出相应的日历牌。
实验目的:掌握Calendar类的使用。
实验要求:通过键盘输入年份和月份,程序输出相应的日历牌。
解决方案 »
- 文件目录显示问题
- java小问题
- m-n之间分别用数组显示能被2整除和不能被2整除的数,用二维数组怎么实现?
- |||||如何在一个java应用程序中打开另一个已打成jar包的应用程序?参与用分|||||||||||||||||||||||||||||||
- Thinking in java2 里的一个包的问题。
- 我C++学的很好,却学JAVA总是不行,难道JAVA真是一门非常简单的语言,还是我太笨?
- [Eclipse]checkstyle-3.1如何安装?
- 请问java怎么做金额线?
- 用户权限的具体实现不可不看!!高手请进!!!
- String是类还是对象?
- 数组中的反转小问题
- 报错“java.lang.ClassNotFoundException”
//private static int month; public static void main(String[] args) {
// 请输入您要显示的年份和当年第一天的星期
String yearString = JOptionPane.showInputDialog(
"请输入您要" +"显示的年份,类型为int:");
int year = Integer.parseInt(yearString); String firstDayString = JOptionPane
.showInputDialog("请输入该年第一天的星期,范围为0-6:");
int firstDay = Integer.parseInt(firstDayString);
int startDay = firstDay;
int numberOfDaysInMonth = 0;// 每月的天数
for (int month = 1; month <= 12; month++) {
System.out.print(" ");
switch(month){
case 1:
System.out.println(year+"January ");
numberOfDaysInMonth = 31;
break;
case 2:
System.out.println("Feburary " + year);
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
numberOfDaysInMonth = 29;
else
numberOfDaysInMonth = 28;
break;
case 3:
System.out.println("March " + year);
numberOfDaysInMonth = 31;
break;
case 4:
System.out.println("April " + year);
numberOfDaysInMonth = 30;
break;
case 5:
System.out.println("May " + year);
numberOfDaysInMonth = 31;
break;
case 6:
System.out.println("June " + year);
numberOfDaysInMonth = 30;
break;
case 7:
System.out.println("July " + year);
numberOfDaysInMonth = 31;
break;
case 8:
System.out.println("August " + year);
numberOfDaysInMonth = 31;
break;
case 9:
System.out.println("September " + year);
numberOfDaysInMonth = 30;
break;
case 10:
System.out.println("October " + year);
numberOfDaysInMonth = 31;
break;
case 11:
System.out.println("November " + year);
numberOfDaysInMonth = 30;
break;
case 12:
System.out.println("December " + year);
numberOfDaysInMonth = 31;
break;
}
System.out.println("==============================");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat"); // 显示输出
int i = 0;
for (i = 0; i < startDay; i++){
System.out.print(" ");
}
for (i = 1; i <= numberOfDaysInMonth; i++) {
if (i < 10)
System.out.print(" " + i);
else
System.out.print(" " + i);
// 每行排7天,整除7就换行
if ((i + startDay) % 7 == 0)
System.out.println();
}
System.out.println();
System.out.println();
// 对下个月的第一天进行初始化赋值
startDay = (startDay + numberOfDaysInMonth) % 7;
}
}
}
import java.util.*;class CalendarTest
{
public static void main(String[] args) throws Exception
{
System.out.print("输入年份: ");
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
int year = Integer. parseInt(br1.readLine());
for(int i=1; i<=12; i++)
{
System.out.println("*******************************************************");
System.out.println("*\t\t"+year+"年"+i+"月\t\t\t");
System.out.println("*******************************************************");
prin(year, i);
System.out.println();
System.out.println();
}
} public static void prin(int year, int month)
{
Calendar calendar = new GregorianCalendar();
String week[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
for (int i=0; i<week.length; i++ )
{
System.out.print(week[i]+"\t");
}
System.out.print("\n"); int monthDay = 0; //一个月的天数
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthDay = 31;break;
case 4:
case 6:
case 9:
case 11:
monthDay = 30;break;
case 2:
monthDay = ((year%4==0&&year%100!=0)||(year%400==0))?29:28;
break;
} //System.out.println("monthDay = "+monthDay);
calendar.set(year, month-1,1);//设置第一天 通过week[calendar.get(Calendar.DAY_OF_WEEK)-1]拿到第一天是星期几
int count = 0; //记录空格 while (!(week[calendar.get(Calendar.DAY_OF_WEEK)-1].equals(week[count])))
{
count++;
}
for (int j=0; j<count; j++ )
{
System.out.print("\t");
}
for(int i=1; i<=monthDay; i++)
{
calendar.set(year, month-1,i);
System.out.print(" "+i+"\t");
if(week[calendar.get(Calendar.DAY_OF_WEEK)-1].equals("星期六"))
{
System.out.print("\n");
}
}
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
class 打印日历{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入年份");
int year=sc.nextInt();
System.out.println("请输入月份:");
int month=sc.nextInt(); Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH,month-1);
cal.set(Calendar.DAY_OF_MONTH,1);
//获取星期信息
int week=cal.get(Calendar.DAY_OF_WEEK);
//计算空格数量
int spaceNum=week-1;
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;
}
//输出日历
System.out.println("日\t一\t二\t三\t四\t五\t六");
for(int i=1;i<=(day+spaceNum);i++)
{
if(i<=spaceNum)//打印空格
{
System.out.print("\t");
}
else
{
System.out.print((i-spaceNum)+"\t");
}
if(i%7==0)
{
System.out.println();
}
}
}
}我试过是可以实现的,还有你可以再考虑如何,输入年份就打印出一年的日历,提示,在前面加个循环即可,试试看吧!!!!