特定の年月の月間カレンダー情報を印刷するという機能を実現するアプリケーションプログラムを作成します。たとえば、2019 3と入力すると、次の月間カレンダー情報が出力されます。静的メソッドの
ハイライト
Javaクラスは非静的クラスメンバー(プロパティ、メソッド)を呼び出すことができません
コード
①基本(最適化されていないコード)
// An highlighted block
package calen;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class cal {
public static void main(String args[]) throws ParseException{
System.out.println("请输入年份月份:");
Integer ca[][]=new Integer[6][7]; //存储日历的数组;6 对应日历有六行,7对应日历一周7天。
int x=1; //每月第一天
int year; //年
int month; //月
String day="1"; //日
// 输入年月日
Scanner in = new Scanner(System.in);
year=in.nextInt();
month=in.nextInt();
in.close();
System.out.println("\t"+"\t"+"\t"+year+"年"+month+"月");
System.out.println();
//每月一号对应周几
SimpleDateFormat fmt = new SimpleDateFormat("dd MM yyyy");
Date d = null;
d = fmt.parse(day+" "+month+" "+year);
Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
int weekDay = calendar.get(Calendar.DAY_OF_WEEK)-1;
// 判断不同月份的天数并据此来存储日历表
if(month!=2&&month!=4&&month!=6&&month!=9&&month!=11)
for(int i=0;(i<6)&&(x<=31);i++)
for(int j=0;(j<7)&&(x<=31);j++){
if(i==0 && j<weekDay){
//列数7 对应一周7天 日历一定从第一行开始而且 当列数小于星期天数时,则该位置的的数置空NULL
ca[i][j]=null;
continue; //结束本次循环,不输出后面内容
}
// 当列数=星期天数时,则代表1号从这列开始
ca[i][j]=x; //把每月的第几天天赋给数组存储
x++;
}
else if((month==2)&&(year%4==0&&year%100!=0||year%400==0))
for(int i=0;(i<6)&&(x<=29);i++)
for(int j=0;(j<7)&&(x<=29);j++){
if(i==0 && j<weekDay){
ca[i][j]=null;
continue;
}
ca[i][j]=x;
x++;
}
else if((month==2)&&(year%4!=0||year%100==0||year%400!=0))
for(int i=0;(i<6)&&(x<=28);i++)
for(int j=0;(j<7)&&(x<=28);j++){
if(i==0 && j<weekDay){
ca[i][j]=null;
continue;
}
ca[i][j]=x;
x++;
}
else
for(int i=0;(i<6)&&(x<=30);i++)
for(int j=0;(j<7)&&(x<=30);j++){
if(i==0 && j<weekDay){
ca[i][j]=null;
continue;
}
ca[i][j]=x;
x++;
}
//输出日历
System.out.println("日 一 二 三 四 五 六");
for(int i=0;i<6;i++) {
for(int j=0;j<7;j++)
{
System.out.print(" "+ca[i][j]+"\t");
}
System.out.println();
}
}
}
コードの最適化
メソッドを使用して、さまざまな関数のコードブロックを分離します。
// An highlighted block
package calen;
//导入模块
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class cal {
static Integer ca[][]=new Integer[6][7];
static int x=1;
static int year;
static int month;
static String day="1";
static int weekDay;
//输入日期
static void input(){
System.out.println("请输入年份月份:");
Scanner in = new Scanner(System.in);
year=in.nextInt();
month=in.nextInt();
in.close();
System.out.println("\t"+"\t"+"\t"+year+"年"+month+"月");
System.out.println();
}
//计算每月第一天对应周几
static void Weekday() throws ParseException {
// 2
SimpleDateFormat fmt = new SimpleDateFormat("dd MM yyyy");
Date d = null;
d = fmt.parse(day+" "+month+" "+year);
Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
weekDay = calendar.get(Calendar.DAY_OF_WEEK)-1;
}
//判断不同月份天数
static void Month() {
// x的取值范围不同, 其余过程相同
if(month!=2&&month!=4&&month!=6&&month!=9&&month!=11)
// value=31
calcul(31); //计算日历
else if((month==2)&&(year%4==0&&year%100!=0||year%400==0))
// value=29
calcul(29); //计算日历
else if((month==2)&&(year%4!=0||year%100==0||year%400!=0))
// value=28
calcul(28); //计算日历
else
// value=30
calcul(30); //计算日历
}
//计算日历
static int calcul(int value){
for(int i=0;(i<6)&&(x<=value);i++)
for(int j=0;(j<7)&&(x<=value);j++){
if(i==0 && j<weekDay){
ca[i][j]=null;
continue;
}
ca[i][j]=x;
x++;
}
return 0;
}
//输出日历
static void display(){
System.out.println("日 一 二 三 四 五 六");
for(int i=0;i<6;i++) {
for(int j=0;j<7;j++)
{
System.out.print(" "+ca[i][j]+"\t");
}
System.out.println();
}
}
public static void main(String args[]) throws ParseException {
input(); // 输入日期
Weekday(); //计算每月第一天对应周几
Month(); //判断不同月份天数
display(); //输出日历
}
}