繁体   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