我正在为我的编程课编写一个程序,其目标是计算输入的日期和月份的星期几。我必须在不使用 Calendar 类的情况下执行此操作。
我的代码适用于我提供的任何月/年,本月(7/2013)除外。我不知道为什么会这样。有人可以帮忙吗?输出示例如下:
错误的例子
run:
Gregorian Calendar Creator
Enter a month: 7
Enter a year: 2013
0=Sun, 1=Mon, 2=Tues, 3=Wed, 4=Thu, 5=Fri, 6=Sat
Test this in a UNIX terminal by typing 'cal July 2013' or similar date
First day of the month: 2
First day of the year: 1
BUILD SUCCESSFUL (total time: 5 seconds)
正确的例子
run:
Gregorian Calendar Creator
Enter a month: 7
Enter a year: 2012
0=Sun, 1=Mon, 2=Tues, 3=Wed, 4=Thu, 5=Fri, 6=Sat
Test this in a UNIX terminal by typing 'cal July 2013' or similar date
First day of the month: 1
First day of the year: 0
BUILD SUCCESSFUL (total time: 5 seconds)
package calendar;
/**
*
* @author Michael Dornisch
*/
import java.util.Scanner;
public class Calendar {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Initiating vars
int inMonth;
int inYear;
//Initiating Scanner
Scanner input = new Scanner(System.in);
System.out.println("Gregorian Calendar Creator"); //Just a print line
/* This block gets a month. Instead of exiting when the user inputs an
* invalid month, it instead asks again untl a proper month is used.
*/
System.out.print("Enter a month: ");
inMonth = input.nextInt();
while (inMonth > 12 || inMonth <= 0) {
System.out.println("ERROR: Improper month. Please enter a proper numerical month (1-12).");
System.out.print("Enter a month: ");
inMonth = input.nextInt();
}
/* This block gets a year. Instead of exiting when the user inputs an
* invalid year, it instead asks again untl a proper year is used.
*/
System.out.print("Enter a year: ");
inYear = input.nextInt();
while (inYear < 1752) {
System.out.println("ERROR: Improper Year. Please enter a year that is 1753 or greater.");
System.out.print("Enter a year: ");
inYear = input.nextInt();
}
/* Here is where I am testing everything
*
*/
Calendar newCal = new Calendar();
System.out.println("0=Sun, 1=Mon, 2=Tues, 3=Wed, 4=Thu, 5=Fri, 6=Sat");
System.out.println("Test this in a UNIX terminal by typing 'cal July 2013' or similar date");
System.out.println("First day of the month: " + newCal.firstDayOfMonth(inMonth, inYear));
System.out.println("First day of the year: " + newCal.firstDayOfYear(inYear));
}
public boolean leap(int inputYear) {
/* This IF is checked first. If the year is divisible by 100, it isn't a
* leap year unless it is also divisible by 400.
*/
if (inputYear % 100 == 0) {
if (inputYear % 400 == 0) {
return (true);
} else {
return (false);
}
}
//Any other number that's divisible by 4 is also a leap year
if (inputYear % 4 == 0) {
return (true);
} else {
return (false);
}
}
public int firstDayOfYear(int inputYear) {
/*
* Here is a formula for finding the day of the week for ANY date.
* N = d + 2m + [3(m+1)/5] + y + [y/4] - [y/100] + [y/400] + 2
* N = day of week
* d = day of month
* m = number of month
* y = year
* mod 7 for answer
* 0 = sunday, 1 = monday, etc.
*/
int day = 5 + (6 / 5) + inputYear + (inputYear / 4) - (inputYear / 100) + (inputYear / 400);
day = day % 7;
return (day);
}
public int firstDayOfMonth(int inputMonth, int inputYear) {
/*
* Here is a formula for finding the day of the week for ANY date.
* N = d + 2m + [3(m+1)/5] + y + [y/4] - [y/100] + [y/400] + 2
* N = day of week
* d = day of month
* m = number of month
* y = year
* mod 7 for answer
* 0 = sunday, 1 = monday, etc.
*/
int day = 3 + (2 * inputMonth) + ((3 * (inputMonth + 1)) / 5) + inputYear + (inputYear / 4) - (inputYear / 100) + (inputYear / 400);
day = day % 7;
return (day);
}
}