我正在尝试用Java制作日历。我的目标是让它显示月,日和年。到目前为止,我已经能够做到这一点,但我没有将这些信息放在我所制作的日历中。我真的很新(第二周),所以如果代码很乱我很抱歉,但我真的需要帮助! 这是我到目前为止所拥有的,
CODE:
import java.util.*;
import java.util.Scanner;
public class realcalendar {
public static void drawMonth() {
for (int w = 1 ; w <= 5 ; w++) {
for (int n = 1; n <= 7; n++) {
for (int i = 1; i <= 1; i++) {
System.out.print("+");
for (int j = 1; j <=6; j++) {
System.out.print("-");
}
}
}
System.out.print("+");
System.out.println();
for (int r = 1; r<= 1; r++) {
for (int t = 1; t<= 3; t++) {
for (int y = 1; y<= 8; y++) {
for (int m = 1; m <= 1; m++) {
System.out.print("|");
for (int b = 1; b<=1; b++) {
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
for (int n = 1; n <= 7; n++) {
for (int i = 1; i <= 1; i++) {
System.out.print("+");
for (int j = 1; j <=6; j++) {
System.out.print("-");
}
}
}
System.out.print("+");
System.out.println();
}
private static int numDays = 0;
private static int h = 0;
public static boolean leap(int year) {
if(((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) {
return true;
}
else {
return false;
}
}
public static void firstDayOfYear(int year) {
int month = 13;
year--;
h = (1 + (int)(((month + 1) * 26) / 10.0) + year + (int)(year / 4.0) + 6 * (int)(year / 100.0) + (int)(year / 400.0)) % 7;
String dayName = "";
switch(h){
case 0: dayName = "Saturday"; break;
case 1: dayName = "Sunday"; break;
case 2: dayName = "Monday"; break;
case 3: dayName = "Tuesday"; break;
case 4: dayName = "Wednesday"; break;
case 5: dayName = "Thursday"; break;
default: dayName = "Friday"; break;
}
}
public static void firstDayOfMonth(int month, int year) {
if(month == 1 || month == 2) {
month += 12;
year--;
}
h = (1 + (int)(((month + 1) * 26) / 10.0) + year + (int)(year / 4.0) + 6 * (int)(year / 100.0) + (int)(year / 400.0)) % 7;
String dayName = "";
switch(h){
case 0: dayName = "Saturday"; break;
case 1: dayName = "Sunday"; break;
case 2: dayName = "Monday"; break;
case 3: dayName = "Tuesday"; break;
case 4: dayName = "Wednesday"; break;
case 5: dayName = "Thursday"; break;
default: dayName = "Friday"; break;
}
}
public static void numDaysInMonth(int month, int year){
int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month == 2 && leap(year)) days[month] = 29;
numDays = days[month];
System.out.println("The number of days in the month is " + numDays);
System.out.println();
}
public static void printCal(int month, int year){
String[] monthNames = {"","January","February","March","April","May","June","July","August","September","October","November","December"};
System.out.println(" " + monthNames[month] + " " + year);
System.out.println("Su Mo Tu We Th Fr Sa");
for (int i = 1; i <= 6; i++)
System.out.print(" ");
for (int i = 1; i <= numDays; i++){
System.out.printf("%2d ", i);
if (((i + h - 1) % 7 == 0) || (i == numDays))
System.out.println();
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter month (1-12): ");
System.out.println();
int month = input.nextInt();
if(month < 1 || month > 12){
System.out.println("Invalid month. Valids inputs are 1-12.");
System.exit(0);
}
System.out.print("Enter year: ");
System.out.println();
int year = input.nextInt();
if(year < 1753){
System.out.println("Invalid year. Valid inputs are 1753 and beyond.");
System.exit(0);
}
firstDayOfYear(year);
firstDayOfMonth(month, year);
numDaysInMonth(month, year);
printCal(month, year);
System.out.println();
drawMonth();
}
}
输出: http://imgur.com/gallery/hLMoO
我希望它看起来像: http://imgur.com/gallery/MtCuMNu
再次,抱歉代码,但任何帮助将不胜感激。
答案 0 :(得分:1)
问题是你有两个不同的函数一个接一个地写输出。第一个是打印实际的日历数据,第二个是写“背景”。使用文本输出,您不能只打印另一个,您必须以某种方式合并它们。
但首先我想给你一些有助于分析和维护代码的一般编码技巧,然后我会提出我对主要问题的答案。
如果所有变量都有一个有意义的名称或至少对它们的含义进行评论(例如'h'变量 - 在begginig我不知道它是什么,那么分析你的代码会更容易。 关于每个循环,逻辑语句和方程式正在做什么的一些评论也可能有所帮助。
答案。 请注意我尽可能多地使用原始代码,以便大致了解如何解决问题。
我的主张是首先生成一个数组来表示当月的日期,然后将它们插入输出的“单元格”中,我将使用它而不是“ printCal < / em>的“:
public static int[][] genCal(int month, int year, int firstDay){
int[][] calendarData = new int [6][7]; //6 weeks x 7 days of week
int currentWeek=0;//we are starting populating the calendarData from its first week
int currentWeekDay= firstDay; //we are starting populating the calendarData with first day of month
for (int i = 1; i <= numDays; i++){
calendarData[currentWeek][currentWeekDay] = i;
currentWeekDay++;
if ( (i + h - 1) % 7 == 0) {
currentWeek += 1; //setting row number to the next week
currentWeekDay = 0; // it's next week so we are starting from it's first day
}
}
return calendarData;
}
为了使用它,我需要传递给它 firstWeekDay ,或者月份的哪一天开始(星期日是0,星期六6),我稍微修改了你的 firstDayOfMonth( )返回该值(你的'h'0被分配到星期六,但我需要一周的第一天是星期日,所以无论如何我都要解析它):
public static int firstDayOfMonth(int month, int year) {
if(month == 1 || month == 2) {
month += 12;
year--;
}
h = (1 + (int)(((month + 1) * 26) / 10.0) + year + (int)(year / 4.0) + 6 * (int)(year / 100.0) + (int)(year / 400.0)) % 7;
String dayName = "";
switch(h){
case 0: {dayName = "Saturday"; return 6;}
case 1: dayName = "Sunday"; return 0;
case 2: dayName = "Monday"; return 1;
case 3: dayName = "Tuesday"; return 2;
case 4: dayName = "Wednesday"; return 3;
case 5: dayName = "Thursday"; return 4;
default: dayName = "Friday"; return 5;
}
}
现在主要问题的下一部分:我们必须编辑 drawMonth(),以便从日历中插入值。你的函数版本有一些不必要的循环,并且不太冗,所以我决定完全重写它。 我使用了不同的输出格式,所以你必须自己再次编写它,但我写了它(真的很快),让你对如何完成它有一个大概的想法:
public static void drawMonth(int[][] calendarData) {
for (int[] week : calendarData) { //for each row (week) of the month
{
for (int i = 0; i < 7; i++)
System.out.print("=====");
System.out.println();
for (int day : week) //for each day of the week
if (day!=0) System.out.printf("|%2d ", day);
else System.out.print("| ");
System.out.println("|");
}
}
for (int i = 0; i < 7; i++)
System.out.print("=====");
}
为了导航' calendarData '数组,我使用了foreach循环,但如果您愿意,可以使用普通的for循环来实现。
最后,你必须在主方法中将它粘在一起,例如在最后添加这样的东西:
int firstDay = firstDayOfMonth(month, year);
int[][] calendarData = genCal(month, year, firstDay);
drawMonth(calendarData);