Java练习:在网格中生成当前月份的日历输出

Java练习:在网格中生成当前月份的日历输出,java,arrays,algorithm,Java,Arrays,Algorithm,我正在做一个小练习来测试自己。我制作了一个小程序来打印当月的日期。我假设您将获得当前日期,例如5月10日和当前日期星期四。我的问题是,有没有可能在没有给出日期和日期的情况下做这个练习?这是我的密码: public static void main(String [] args) { int days = 31; // may String arr[] = new String[]{"Mon", "Tue", "Wed", "Thur", "Fri","Sat",

我正在做一个小练习来测试自己。我制作了一个小程序来打印当月的日期。我假设您将获得当前日期,例如5月10日和当前日期星期四。我的问题是,有没有可能在没有给出日期和日期的情况下做这个练习?这是我的密码:

public static void main(String [] args) {
        int days = 31; // may
        String arr[] = new String[]{"Mon", "Tue", "Wed", "Thur", "Fri","Sat", "Sun"};
        int today_date = 10;
        String today_day = "Thur";
        int today_index = 0;

        //get index of current day e.g. Thur = index 3
        for(int i = 0; i<arr.length; i++) {
            if(today_day.equals(arr[i])) {
                today_index = i;
            }
        }
        //count from today_day to 1, the purpose is to get the first day of the month
        while(today_date > 1) {
            if(today_index == 0) {
                today_index = 6;
                today_date--;
            }
            else {
                today_date--;
                today_index--;
            }
        }
        //print each day of the month
        for(int i = 1; i<=days; i++ ) {
            if(today_index == 6) {
                System.out.print(i+"("+ arr[today_index]+") \n");
                today_index = 0;
            }
            else {
                System.out.print(i+"("+ arr[today_index]+") | ");
                today_index++;
            }
        }

    }
publicstaticvoidmain(字符串[]args){
整数天=31;//五月
字符串arr[]=新字符串[]{“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”};
int today_date=10;
String today=“Thur”;
今天的int_指数=0;
//获取当天的索引,例如,星期四=索引3
对于(int i=0;i

  • 创建键值对,用于映射“整数到月份名称”和“整数到一周中的某一天”。(也可以使用开关盒)

  • 使用java.util包中的日历,获取给定日期、月份和年份的星期几

  • 例:

  • 使用“整数到一周中的某一天”键值对以文字形式打印相应的日期和月份

  • 希望这能有所帮助。

    我的问题是什么意思,你能在没有给出日期和日期的情况下做这个练习吗???例如,如果没有给出10(5月10日)和周四(今天)。如果只给出“可能”,为什么不直接使用
    Calendar.getInstance()获取当前日期
    ?@DamCx感谢您的回复,本练习的目的是不使用Calendar类。您必须固定一个特定的日期作为参考,在Java中,日期0是,因此您可以使用类似的策略。
    Calendar cal = new GregorianCalendar();
        cal.set(Calendar.MONTH, 4);
        cal.set(Calendar.DAY_OF_MONTH, 15);
        cal.set(Calendar.YEAR, 2018);
        System.out.println(cal.getTime());
        System.out.println(cal.getDisplayName(2, 3, Locale.US));