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

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 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型