如何在数组/矩阵中设置一组特定的数字进行打印?

时间:2013-10-16 03:47:57

标签: java arrays matrix calendar

我知道这很邋and,我打算在将其作为作业转发之前对其进行打磨。但是,我是Java的初学者,当我输入两个数字时,我需要打印一个特定月份的日历。我对矩阵,阵列等有一点把握,但我只在这工作了两个星期,我觉得很烦。如果有人可以向我解释我需要如何格式化矩阵以便它只显示特定年份所选月份的特定天数,那就太棒了(即使你只是建议一种方法)。我不一定要为我的代码寻找一个特定的答案(虽然我也会这样做)。

package javaapplication2;
import java.util.Scanner;
/**
*
*/
public class JavaApplication2 {
 /**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

Scanner input = new Scanner(System.in);
// Declare variables
int month;
int year;
String yeartext;

// Ask for variable amount
    {System.out.println("Please provide the year in numerical form");
     year = input.nextInt(); 

    System.out.println("Please provide the month in numerical form");
    month = input.nextInt();
// check if is leap year
     if (isLeapYear(year)) System.out.println("is a leap year");
     }

    String GetMonthNameNow;
            GetMonthNameNow = getMonthName(month);
    int startofmonthtext;
        startofmonthtext = getStartDay(year, month);
    int daystext;
        daystext = getNumOfDaysInMonth(year, month); 
    long totaltext;
            totaltext = getTotalNumOfDays(year, month);
            yeartext = String.valueOf(year);

    int twoDm[][]= new int[5][7];
    int i,j,k=1;
        for(i=0;i<5;i++) {
        for(j=0;j<7;j++) {
            twoDm[i][j]=k;
            k++;
    }
}

    String [][] CalendarArray = {
        {"------------------------------------------" },
        {" ", " ", " ", " ", " ", " ", " ", " ", " ", },
        };

System.out.println(GetMonthNameNow + ", " + yeartext);
System.out.println(CalendarArray[0][0]);
System.out.println("Sun Mon Tue Wed Thu Fri Sat");

for(int[] row : twoDm) {
        printRow(row);
    }
        System.out.println("");
        System.out.println("");
System.out.println(" " + daystext + " " + startofmonthtext + " " + 
        totaltext);
}

//set up the println format for the days array
public static void printRow(int[] row) {
    for (int i : row) {
        System.out.print(i + " ");
        System.out.print(" ");
    }
    System.out.println();
}


//Check to see if year is a Leap Year
public static boolean isLeapYear(int year) {
    if (year % 4==0)
    return true;
    else return false;
    }

//Check the month number with the correlating month name
public static String getMonthName(int month) {
    String monthstext = null;

    String[] months = {"", "January", "February", "March", "April", "May", 
        "June", "July", "August", "September", "October", "November", 
        "December"};
    if (month == 1)
        return months [1];
    else if (month ==2)
        return months [2];
    else if (month ==3)
        return months [3];
    else if (month ==4)
        return months [4];
    else if (month ==5)
        return months [5];
    else if (month ==6)
        return months [6];
    else if (month ==7)
        return months [7];
    else if (month ==8)
        return months [8];
    else if (month ==9)
        return months [9];
    else if (month ==10)
        return months [10];
    else if (month ==11)
        return months [11];
    else if (month ==12)
        return months [12];
    return monthstext;

}

//get number of days in specific months and declare 31 for general months
public static int getNumOfDaysInMonth(int year, int month) {
         int days;      

         if (month == 2){
             days = 28;
         if (isLeapYear(year))
             days = 29;}

         else if (month == 4)
             days = 30;
         else if (month == 4)
             days = 30;
         else if (month == 6)
             days = 30;
         else if (month == 9)
             days = 30;
         else if (month == 11)
             days = 30;
         else
             days = 31;
         return days;              

         }


/** Get the start day of the first day in a month */
public static int getStartDay(int year, int month) {
// Get total number of days since 1/1/1800
int startDay1800 = 3;
long totalNumOfDays = getTotalNumOfDays(year, month);
// Return the start day    
return (int)((totalNumOfDays + startDay1800) % 7);
}    
 //Calculate the total number of days that have passed since 
//1/01/1800 and the user entered month/year
   public static long getTotalNumOfDays(int year, int month) {
    long total = 0;
    // Get the total days from 1800 to year -1
    for (int i = 1800; i < year; i++)
    if (isLeapYear(i))
    total = total + 366;
    else
    total = total + 365;
    // Add days from Jan to the month prior to the calendar month
    for (int i = 1; i < month; i++)
    total = total + getNumOfDaysInMonth(year, i);
    return total;
}
}

1 个答案:

答案 0 :(得分:0)

我已经在两个地方更改了您的代码:

int twoDm[][]= new int[5][7];
int i,j,k=1;
int skipped = 0;
outer:
for(i=0;i<5;i++) {
    for(j=0;j<7;j++) {
        if (skipped < startofmonthtext) {
            skipped ++;
            continue;
        }
        twoDm[i][j]=k;
        k++;
        if (k > daystext) break outer;
    }
}

这会跳过设置2D数组中的前几个条目以考虑一周中月份的开始。当我们通过break从外部for循环中添加足够的天数时,它也会停止填充2D数组。

和...

public static void printRow(int[] row) {
    for (int i : row) {
        String str = null;
        if (i == 0) str = "  "; // 0 = don't show
        else if (i < 10) str = " " + i; // 1 digit
        else str = "" + i; // 2 digits
        System.out.print(str + " ");
        System.out.print(" ");
    }
    System.out.println();
}

这只是显示空格而不是0条目和填充< 10条目以及额外的空格。现在输出格式很好。

但是,实际天数与我的真实日历不符,所以你应该考虑一下......

祝你好运。