返回各个ascii日历位置中的多维数组项

62lalag4  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(162)

我正在尝试添加在我创建的日历应用程序上查看事件的功能。在启动时,当用户在菜单中键入“ev”时,他们可以添加带有月、日和标题的事件,所有这些都将添加到[12][monthlength]多维数组中的相应位置。一旦他们将事件添加到数组中,当显示某个月时,事件所在的框应该显示事件的标题,最后一位就是我卡住的地方。
以下是通过用户输入(ev按钮)添加数据的数组:

public static Object[][] EVENTS = new Object[13][31];

代表12个月,每个月31天。
打印出的数组如下所示:

[[null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null], [null, EVENT, TITLE, null, NOTHER, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]]

虽然这看起来很混乱,但如果您查看第三个子数组,您可以看到通过用户输入添加的示例数据。
最后,试图在日历上显示这些数据是事情变得复杂的地方。这是我的密码:

public static void drawRow(int week, int days, boolean firstWeek, int startDay, int month) {

        header(SIZE * 7);

        // this is for the firstt row of each row. includes day
        // draws each box for week length
        // if current loop count < start day, do an empty box
        //
        if (firstWeek == true) {
            // deals with height, days passed = length
            for (int i = 1; i <= SIZE / 2; i++) {
                if (startDay != 1) {
                    System.out.print("|");
                }

                spaceRow(startDay - 1, true, startDay, month, week);
                if (i == 1) {
                    numRow(week, days - startDay + 1, true);
                    System.out.print("|");
                } else {
                    spaceRow(days - startDay, true, startDay, month, week);

                    for (int last = 1; last <= SIZE - 1; last++) {
                        System.out.print(" ");
                    }
                    System.out.print("|");
                }
                System.out.println();
            }

        } else {

            numRow(week, days, false);

            System.out.println("|");
            // this is for the rest (without numbers)
            // height of each box
            for (int height = 1; height <= SIZE / 2 - 1; height++) {
                spaceRow(days, false, startDay, month, week);
            }
        }

    }

    public static void numRow(int week, int days, boolean first) {
        for (int col = 1; col <= days; col++) {
            if (first == true && col == 1) {
                System.out.print("");
            } else {
                System.out.print("|");
            }
            // get length of current days number
            String weekStr = String.valueOf(week);
            int length = weekStr.length();

            if (week <= 31) {

                System.out.print(week++);

            } else {
                System.out.print("  ");
            }

            for (int space = 1; space <= SIZE - length - 1; space++) {
                System.out.print(" ");
            }
        }
    }

    public static void spaceRow(int days, boolean first, int startDay, int mnth, int week) {
        //
        // makes each row (full length)

        for (int col2 = 1; col2 <= days; col2++) {
            // use week -1 to get correct day
            week++;
            if (first == true && col2 == 1) {

            } else {
                System.out.print("|");
            }
            //
            if (EVENTS[mnth][week - 1] != null) {
                System.out.print("x");
            } else {
                for (int space = 1; space <= SIZE - 1; space++) {
                    System.out.print(" ");
                }
            }

        }
        if (startDay == 7) {
            System.out.print("|");
        } else {
            System.out.print("|");
        }
        // if its the last row being drawn (when first ==true), dont go to the next row

        if (first == false) {
            System.out.println();
        }
    }

    // division between weeks (=====)
    public static void header(int sz) {
        for (int top = 1; top <= sz; top++) {
            System.out.print("=");
        }
        System.out.println();
    }

有更多的代码比这个,但我认为这是足够的,得到一个什么是正在发生的想法。如果你往下看3/4的地方,你会看到“如果(事件[mnth][week-1]”这是我试图保持逻辑来决定一个事件是否应该放在空白处x“)只是为了测试的目的,但现在我得到一个索引超出范围的错误。
以下是日历,了解更多内容:

__//
  /.__.\
  \ \/ /
`_/    \
 \-     )
  \____/ 
   | |         /
======================================================================
        \    \           \
                                  11 

 SUN       MON       TUE       WED       THU       FRI       SAT
======================================================================
|         |         |         |         |         |1        |2        |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
======================================================================
|3        |4        |5        |6        |7        |8        |9        |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
======================================================================
|10       |11       |12       |13       |14       |15       |16       |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
======================================================================
|17       |18       |19       |20       |21       |22       |23       |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
======================================================================
|24       |25       |26       |27       |28       |29       |30       |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
|         |         |         |         |         |         |         |
======================================================================
Month: 11
Day: 2

Please type a command
'e' to enter a date and display the correspoding calendar 
't' to get todays date and display todays calendar
'n' to display the next month
'p' to display the previous month
'd' to exit the program
'ev' to add an event

我知道这是一个混乱的问题,但任何意见或提示是赞赏。如果你需要更多的信息,我会尽快答复。谢谢您!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题