提示输入年份和月份,然后打印出该月的日历,代码如下:
import java.util.*;
public class Chapter5_34 {
//Main method
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//prompt the user to enter year
System.out.print("Enter full year (e.g.,2001):");
int year = input.nextInt();
//prompt the user to enter month
System.out.print("Enter month in number between 1 and 12:");
int month = input.nextInt();
printMonth(year,month);
}
public static void printMonth(int year , int month){
//print the headings of the calendar
printMonthTitle(year , month);
//print the body of the calendar
printMonthBody(year , month);
}
/**print the month title,e.g.,may , 1999*/
public static void printMonthTitle(int year , int month){
System.out.println(" " + getMonthName(month) + " " + year);
System.out.println(" ---------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
/**
* Get the English name for the month
*/
public static String getMonthName(int month){
String monthName = " ";
switch (month) {
case 1 : monthName = "January";break;
case 2 : monthName = "February";break;
case 3 : monthName = "March";break;
case 4 : monthName = "April";break;
case 5 : monthName = "May";break;
case 6 : monthName = "June";break;
case 7 : monthName = "July";break;
case 8 : monthName = "August";break;
case 9 : monthName = "September";break;
case 10 : monthName = "October";break;
case 11 : monthName = "November";break;
case 12 : monthName = "December";break;
}
return monthName;
}
/**
* Print month body
*/
public static void printMonthBody(int year , int month){
//Get start day of the week fot the first date in the month
int startDay = getStartDay(year , month);
//Ger number of days in the month
int numberOfDaysInMonth = getNumberOfDaysInMonth(year , month);
//Pad space before the first day of the month
int i = 0;
for(i = 0;i < startDay;i++){
System.out.print("");
for(i = 1;i <=numberOfDaysInMonth;i++){
System.out.printf("%4d", i);
if((i + startDay)%7 == 0)
System.out.println();
}
System.out.println();
}
}
public static int getStartDay(int year ,int month){
int j , k , h;
j = year / 100;
k = year % 100;
h = (1 + (26*(month + 1)/10) + k + (k/4) + (j/4) + 5*j)%7;
return h+7;
}
private static int getNumberOfDaysInMonth(int year, int month) {
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 10 || month ==12)
return 31;
if(month == 4 || month == 6 || month == 9 || month == 11)
return 30;
if(month == 2)return isLeapYear(year) ? 29:28;
return 0;//if month is incorrect
} private static boolean isLeapYear(int year) {
return year % 400 == 0 || (year % 4 ==0 && year % 100 !=0);
}
}
然后我得到的格式一直是这样:
不知道要怎么改才好,大家指导一下。
论坛没分了,大家见谅啊!
import java.util.*;
public class Chapter5_34 {
//Main method
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//prompt the user to enter year
System.out.print("Enter full year (e.g.,2001):");
int year = input.nextInt();
//prompt the user to enter month
System.out.print("Enter month in number between 1 and 12:");
int month = input.nextInt();
printMonth(year,month);
}
public static void printMonth(int year , int month){
//print the headings of the calendar
printMonthTitle(year , month);
//print the body of the calendar
printMonthBody(year , month);
}
/**print the month title,e.g.,may , 1999*/
public static void printMonthTitle(int year , int month){
System.out.println(" " + getMonthName(month) + " " + year);
System.out.println(" ---------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
/**
* Get the English name for the month
*/
public static String getMonthName(int month){
String monthName = " ";
switch (month) {
case 1 : monthName = "January";break;
case 2 : monthName = "February";break;
case 3 : monthName = "March";break;
case 4 : monthName = "April";break;
case 5 : monthName = "May";break;
case 6 : monthName = "June";break;
case 7 : monthName = "July";break;
case 8 : monthName = "August";break;
case 9 : monthName = "September";break;
case 10 : monthName = "October";break;
case 11 : monthName = "November";break;
case 12 : monthName = "December";break;
}
return monthName;
}
/**
* Print month body
*/
public static void printMonthBody(int year , int month){
//Get start day of the week fot the first date in the month
int startDay = getStartDay(year , month);
//Ger number of days in the month
int numberOfDaysInMonth = getNumberOfDaysInMonth(year , month);
//Pad space before the first day of the month
int i = 0;
for(i = 0;i < startDay;i++){
System.out.print("");
for(i = 1;i <=numberOfDaysInMonth;i++){
System.out.printf("%4d", i);
if((i + startDay)%7 == 0)
System.out.println();
}
System.out.println();
}
}
public static int getStartDay(int year ,int month){
int j , k , h;
j = year / 100;
k = year % 100;
h = (1 + (26*(month + 1)/10) + k + (k/4) + (j/4) + 5*j)%7;
return h+7;
}
private static int getNumberOfDaysInMonth(int year, int month) {
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 10 || month ==12)
return 31;
if(month == 4 || month == 6 || month == 9 || month == 11)
return 30;
if(month == 2)return isLeapYear(year) ? 29:28;
return 0;//if month is incorrect
} private static boolean isLeapYear(int year) {
return year % 400 == 0 || (year % 4 ==0 && year % 100 !=0);
}
}
然后我得到的格式一直是这样:
不知道要怎么改才好,大家指导一下。
论坛没分了,大家见谅啊!
解决方案 »
- 弹冠相庆,Java 6 Update 10 Beta 问世。
- "java -Dfile.encoding=GBK class 中文字符串" 传入的中文string 显示部正确
- 我这个程序里怎么向这样写e.getSource()==td1有点问题呀,到底是哪个地方有问题?
- 用正则式在一个字串里怎么查找并输出YYYY-XX-ZZ格式的日期,求其简单的正则式-初学者
- 如何将Applet转换为Application
- 如何反编译inner class
- jdbc的奇怪限制,不能reread row data
- jdbc紧急求助
- 怎样在java里四舍五入?
- 谢谢大家,请问如何把计算机名或服务器名转换成IP地址在Java中?
- 咨询个内部类的问题
- mynotepad.this是什么意思?
Scanner input = new Scanner(System.in);
//prompt the user to enter year
System.out.print("Enter full year (e.g.,2001):");
int year = input.nextInt();
//prompt the user to enter month
System.out.print("Enter month in number between 1 and 12:");
int month = input.nextInt();
printMonth(year,month);
}
public static void printMonth(int year , int month){
//print the headings of the calendar
printMonthTitle(year , month);
//print the body of the calendar
printMonthBody(year , month);
}
/**print the month title,e.g.,may , 1999*/
public static void printMonthTitle(int year , int month){
System.out.println(" " + getMonthName(month) + " " + year);
System.out.println(" ---------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
/**
* Get the English name for the month
*/
public static String getMonthName(int month){
String monthName = " ";
switch (month) {
case 1 : monthName = "January";break;
case 2 : monthName = "February";break;
case 3 : monthName = "March";break;
case 4 : monthName = "April";break;
case 5 : monthName = "May";break;
case 6 : monthName = "June";break;
case 7 : monthName = "July";break;
case 8 : monthName = "August";break;
case 9 : monthName = "September";break;
case 10 : monthName = "October";break;
case 11 : monthName = "November";break;
case 12 : monthName = "December";break;
}
return monthName;
}
/**
* Print month body
*/
public static void printMonthBody(int year , int month){
//Get start day of the week fot the first date in the month
//int startDay = getStartDay(year , month);
//Ger number of days in the month
int numberOfDaysInMonth = getNumberOfDaysInMonth(year , month);
//Pad space before the first day of the month
/*
int i = 0;
for(i = 0;i < startDay;i++){
System.out.print("");
for(i = 1;i <=numberOfDaysInMonth;i++){
System.out.printf("%4d", i);
if((i + startDay)%7 == 0)
System.out.println();
}
System.out.println();
}
*/
/*本月第一天前的星期数全部输出为空格*/
int firstDayInTheWeek = getDayOfWeekOfFirstDay(year,month);
//System.out.println(firstDay);
int j = 1;
while(j < firstDayInTheWeek) {
System.out.printf("%4s", "");
j ++ ;
}
/*从本月第一天开始打印日历*/
for(int k = 1;k <= numberOfDaysInMonth;k++) {
System.out.printf("%4s", k);
if((k+firstDayInTheWeek-1) % 7 == 0) System.out.println();
}
}
/*
public static int getStartDay(int year ,int month){
int j , k , h;
j = year / 100;
k = year % 100;
h = (1 + (26*(month + 1)/10) + k + (k/4) + (j/4) + 5*j)%7;
return h+7;
}
*/
private static int getNumberOfDaysInMonth(int year, int month) {
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month ==12)
return 31;
if(month == 4 || month == 6 || month == 9 || month == 11)
return 30;
if(month == 2)return isLeapYear(year) ? 29:28;
return 0;//if month is incorrect
}
private static boolean isLeapYear(int year) {
return year % 400 == 0 || (year % 4 ==0 && year % 100 !=0);
}
/*计算出当前月份的第一天是该星期的第几天*/
private static int getDayOfWeekOfFirstDay(int year,int month) {
Calendar cal = new GregorianCalendar(year,month-1,1);
return cal.get(Calendar.DAY_OF_WEEK);
}
}