簡體   English   中英

如何在要打印的數組/矩陣中設置一組特定的數字?

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

我知道這很草率,因此我打算在上交作業之前對其進行拋光。 但是,我是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;
}
}

我在兩個地方更改了您的代碼:

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 -loop。

和...

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條目並帶有額外的空格。 現在,輸出格式正確。

但是,實際的日期與我的真實日歷不符,因此您可能應該調查一下...

祝好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM