简体   繁体   中英

How can I set a specific set of numbers in an array/matrix to print?

I know this is pretty sloppy, and I plan to polish it before turning it in as an assignment. However, I'm a beginner at Java, and I need to get this to print a calendar for a specific month of a year when I input two numbers. I've got a small grasp on matrix, arrays, etc., but I've only been working at this for two weeks, and I feel pretty burnt out. If someone could explain to me how I need to format the matrix so that it only displays the specific amount of days in a specific year's chosen month, that would be fantastic (even if you're just suggesting a way of doing it). I'm not necessarily looking for a specific answer for my code (although I would take that too).

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;
}
}

I've changed your code in 2 places:

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;
    }
}

This skips setting the first few entries in the 2D array to account for the start of the month in the week. It also stops filling the 2D array when we have added enough days, by break ing out of the outer for -loop.

And...

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();
}

This simply shows spaces instead of 0 entries and pads < 10 entries with an extra space. Now the output is nicely formatted.

However, the actual days don't match my real calendar, so you should probably look into that...

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM