sinat_22228995 2015-04-08 09:08 采纳率: 50%
浏览 2235
已采纳

java日历,其中一种情况多了一个换行,该如何解决呢?

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class rili {
public static final String JANUARY = "January";
public static final String FEBUARY = "Febuary";
public static final String MARCH = "March";
public static final String APRIL = "April";
public static final String MAY = "May";
public static final String JUN = "Jun";
public static final String JULY = "July";
public static final String AUGUST = "August";
public static final String SEPTERMBER = "Septermber";
public static final String OCTOBER = "October";
public static final String NOVEMBER = "November";
public static final String DECEMBER = "December";

private int year;

private int whatDayOnJanuaryOne;

public static void main(String[] args) throws Exception {

    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    for (int i = 0; i < n; i++) {
        int nian = in.nextInt();
        int yue = in.nextInt();
        // System.out.println();
        System.out
                .println("|--------------------------------------------------|");
        System.out.println(nian + "-" + yue);
        rili hd = new rili(nian);
        hd.printMonthOfYear(yue);
        System.out
                .println("|--------------------------------------------------|");
    }
}

public rili(int year) {
    this.year = year;
    try {
        setWhatDayOnJanuaryOne(getJanuaryOne(year));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void printMonthOfYear(int mon) throws Exception {
    if (mon < 1 || mon > 12) {
        return;
    }
    GregorianCalendar now = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = sdf.parse(year + "-" + mon + "-01");
    now.setTime(date);
    int month = now.get(Calendar.MONTH);
    now.set(Calendar.DAY_OF_MONTH, 1);
    int week = now.get(Calendar.DAY_OF_WEEK);
    System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
    for (int i = Calendar.SUNDAY; i < week; i++) {
        System.out.print("\t");
    }
    while (now.get(Calendar.MONTH) == month) {
        int day = now.get(Calendar.DAY_OF_MONTH);
        if (day < 10) {
            System.out.print("" + day + "\t");
        } else {
            System.out.print("" + day + "\t");
        }
        if (week == Calendar.SATURDAY) {
            System.out.println();
        }
        now.add(Calendar.DAY_OF_MONTH, 1);
        week = now.get(Calendar.DAY_OF_WEEK);
    }
    System.out.println();
}

public int getJanuaryOne(int year) throws Exception {
    int[] weekDays = { 0, 1, 2, 3, 4, 5, 6 };
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date dt = sdf.parse(year + "-01-01");
    cal.setTime(dt);
    int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
    if (w < 0)
        w = 0;
    return weekDays[w];
}

protected void printFebuary() {
    if (this.year % 4 == 0) {

        printLeapYear();
    } else {

        printNonleapYear();
    }
}

private void printLeapYear() {
    for (int j = 1; j <= 29; j++) {
        String tmp = "";
        if (j == 1) {
            for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
                tmp = tmp + "      ";
            }
            tmp = tmp + "   " + j + "  ";
            if (this.whatDayOnJanuaryOne == 6) {
                System.out.println(tmp);
            } else {
                System.out.print(tmp);
            }
        } else if (j > 1 && j < 29) {

            if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
                System.out.println("   " + j);
            } else {
                if (j < 10) {
                    System.out.print("   " + j + "  ");
                } else {
                    System.out.print("   " + j + " ");
                }
            }
        } else if (j == 29) {
            System.out.println("   " + j);
            this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
        }
    }
}

private void printNonleapYear() {
    for (int j = 1; j <= 28; j++) {
        String tmp = "";
        if (j == 1) {
            for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
                tmp = tmp + "      ";
            }
            tmp = tmp + "   " + j + "  ";
            if (this.whatDayOnJanuaryOne == 6) {
                System.out.println(tmp);
            } else {
                System.out.print(tmp);
            }
        } else if (j > 1 && j < 28) {

            if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
                System.out.println("   " + j);
            } else {
                if (j < 10) {
                    System.out.print("   " + j + "  ");
                } else {
                    System.out.print("   " + j + " ");
                }
            }
        } else if (j == 28) {
            System.out.println("   " + j);
            this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
        }
    }
}

protected void print30() {
    for (int j = 1; j <= 30; j++) {
        String tmp = "";
        if (j == 1) {
            for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
                tmp = tmp + "      ";
            }
            tmp = tmp + "   " + j + "  ";
            if (this.whatDayOnJanuaryOne == 6) {
                System.out.println(tmp);
            } else {
                System.out.print(tmp);
            }
        } else if (j > 1 && j < 30) {

            if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
                System.out.println("   " + j);
            } else {
                if (j < 10) {
                    System.out.print("   " + j + "  ");
                } else {
                    System.out.print("   " + j + " ");
                }
            }
        } else if (j == 30) {
            System.out.println("   " + j);
            this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
        }
    }
}

protected void print31() {
    for (int j = 1; j <= 31; j++) {
        String tmp = "";
        if (j == 1) {
            for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) {
                tmp = tmp + "      ";
            }
            tmp = tmp + "   " + j + "  ";
            if (this.whatDayOnJanuaryOne == 6) {
                System.out.println(tmp);
            } else {
                System.out.print(tmp);
            }
        } else if (j > 1 && j < 31) {

            if ((this.whatDayOnJanuaryOne + j) % 7 == 0) {
                System.out.println("   " + j);
            } else {
                if (j < 10) {
                    System.out.print("   " + j + "  ");
                } else {
                    System.out.print("   " + j + " ");
                }
            }
        } else if (j == 31) {
            System.out.println("   " + j);
            this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7);
        }
    }
}

public String getMonth(int m) {
    String month = "";
    switch (m) {
    case 1:
        month = JANUARY;
        break;
    case 2:
        month = FEBUARY;
        break;
    case 3:
        month = MARCH;
        break;
    case 4:
        month = APRIL;
        break;
    case 5:
        month = MAY;
        break;
    case 6:
        month = JUN;
        break;
    case 7:
        month = JULY;
        break;
    case 8:
        month = AUGUST;
        break;
    case 9:
        month = SEPTERMBER;
        break;
    case 10:
        month = OCTOBER;
        break;
    case 11:
        month = NOVEMBER;
        break;
    case 12:
        month = DECEMBER;
        break;
    }
    return month;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}

public int getWhatDayOnJanuaryOne() {
    return whatDayOnJanuaryOne;
}

public void setWhatDayOnJanuaryOne(int whatDayOnJanuaryOne) {
    this.whatDayOnJanuaryOne = whatDayOnJanuaryOne;
}

}

第一行输入一个组数n,表示显示日历的组数。 每一组数据包括年份和月份。
例如当输入2015年2月时,就会多一个换行

  • 写回答

2条回答 默认 最新

  • danielinbiti 2015-04-08 09:16
    关注
     public static void main(String[] args) throws Exception {
    
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            int nian = in.nextInt();
            int yue = in.nextInt();
            // System.out.println();
            System.out
                    .println("|--------------------------------------------------|");
            System.out.println(nian + "-" + yue);
            rili hd = new rili(nian);
            hd.printMonthOfYear(yue);
                    if(i==n-1){//判断是否是最后一个
            System.out
                    .print("|--------------------------------------------------|");
                    }else{
            System.out
                    .println("|--------------------------------------------------|");
                    }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化