我知道这很草率,我计划在将其作为作业提交之前对其进行润色。但是,我是 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;
}
}