这是作业。
我试图在1900年1月1日(我们假设是星期一)和用户输入的一年的12月31日之间的第一个月中发现星期日着陆的每次发生。日历扩展名是禁止的。
我以正确的格式返回日期,但它们与我们的讲师提供的示例代码不匹配
在给定的示例中,1902的输入应返回:
1900年4月1日
1900年7月1日
1901年9月1日
1901年12月1日
1902年6月1日
1902年,我的代码返回:
1900年3月1日
1901年1月1日
1901年4月1日
1901年5月1日
1902年2月1日
1902年6月1日
1902年7月1日
import java.util.Scanner;
public class Sundays {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter the ending year: ");
int userInputYear = reader.nextInt();
int[] orderedLengthOfMonthsArray = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String[] orderedNamesOfMonthsArray = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int month = 0;
int dayOfWeek = 1; // initialized to MONDAY Jan 1, 1900 -- Sunday would be #7
int dayOfMonth = 1;
for (int year = 1900; year <= userInputYear; year++) {
for (month = 0; month < orderedLengthOfMonthsArray.length; month++) {
for (dayOfMonth = 1; dayOfMonth <= orderedLengthOfMonthsArray[month]; dayOfMonth++) {
dayOfWeek++;
if (dayOfMonth == 1 && dayOfWeek == 7) {
System.out.println(dayOfMonth + " " + orderedNamesOfMonthsArray[month] + " " + year);
}
if (dayOfWeek == 8) {
dayOfWeek = 1;
}
}
}
}
}
}
答案 0 :(得分:3)
交换if语句和dayOfWeek
的增加。
for (dayOfMonth = 1; dayOfMonth <= orderedLengthOfMonthsArray[month]; dayOfMonth++) {
if (dayOfMonth == 1 && dayOfWeek == 7) {
System.out.println(dayOfMonth + " " + orderedNamesOfMonthsArray[month] + " " + year);
}
dayOfWeek++;
if (dayOfWeek == 8) {
dayOfWeek = 1;
}
}
当您在dayOfMonth for
循环中时,您已经拥有了正确的星期几(最初是1900年1月1日星期一),所以如果您先增加它,那么之后的检查将是不正确的。
答案 1 :(得分:1)
if (dayOfWeek == 7) {
dayOfWeek = 1;
}
所以你的一周有6天?我认为您应该重置为零或在dayOfWeek
为8时重置。
答案 2 :(得分:1)
有人提到Joda-Time魔术......所以这是我使用Joda-Time 2.3的解决方案。
虽然问题(家庭作业)禁止使用添加的库......
我的示例代码使用LocalDate
,只有在完全确定您不关心(a)时间和(b)时区时才应使用此代码。我通常不建议这种方法作为天真的程序员,他们认为他们既不需要时间也不需要区域经常被误解。
// Start
LocalDate start = new LocalDate( 1900, 1, 1 );
// Stop
// Using "half-open" comparison. We care about December 31 of specified year, but we will test for January 1 of year after.
String input = "1930";
int year = Integer.valueOf( input );
year = ( year + 1 ); // Add one to get to next year, for "half-open" approach.
LocalDate stop = new LocalDate( year, 1, 1 );
// Collect each LocalDate where the first of the month is a Sunday.
java.util.List<LocalDate> localDates = new java.util.ArrayList<LocalDate>();
LocalDate localDate = start;
do {
if ( localDate.getDayOfWeek() == DateTimeConstants.SUNDAY ) { // Comparing 'int' primitive values.
localDates.add( localDate ); // Collect this LocalDate instance.
}
localDate = localDate.plusMonths( 1 ); // Move on to next month. Joda-Time is smart about various month-ends and leap-year.
} while ( localDate.isBefore( stop ) ); // "Half-Open" means test "<" rather than "<=".
转储到控制台...
System.out.println( "The First-Of-The-Month days that are Sundays from " + start + " (inclusive) to " + stop + " (exclusive):" );
System.out.println( localDates );
The First-Of-The-Month days that are Sundays from 1900-01-01 (inclusive) to 1931-01-01 (exclusive):
[1900-04-01, 1900-07-01, 1901-09-01, 1901-12-01, 1902-06-01, 1903-02-01, 1903-03-01, 1903-11-01, 1904-05-01, 1905-01-01, 1905-10-01, 1906-04-01, 1906-07-01, 1907-09-01, 1907-12-01, 1908-03-01, 1908-11-01, 1909-08-01, 1910-05-01, 1911-01-01, 1911-10-01, 1912-09-01, 1912-12-01, 1913-06-01, 1914-02-01, 1914-03-01, 1914-11-01, 1915-08-01, 1916-10-01, 1917-04-01, 1917-07-01, 1918-09-01, 1918-12-01, 1919-06-01, 1920-02-01, 1920-08-01, 1921-05-01, 1922-01-01, 1922-10-01, 1923-04-01, 1923-07-01, 1924-06-01, 1925-02-01, 1925-03-01, 1925-11-01, 1926-08-01, 1927-05-01, 1928-01-01, 1928-04-01, 1928-07-01, 1929-09-01, 1929-12-01, 1930-06-01]
答案 3 :(得分:0)
只是为了好玩,虽然您明确提到不使用Calendar
,但我们可以采用以下方式:
public static List<Date> mondaysFirst(int firstYear, int lastYear) {
final List<Date> dates = new ArrayList<>();
final Calendar c1 = Calendar.getInstance(Locale.US);
c1.set(firstYear, 0, 1, 0, 0, 0);
final Calendar c2 = Calendar.getInstance(Locale.US);
c2.set(lastYear, 11, 31, 23, 59, 59);
while (c1.before(c2)) {
final int dayOfTheWeek = c1.get(Calendar.DAY_OF_WEEK);
// is sunday
if (dayOfTheWeek == 1) {
dates.add(c1.getTime());
}
c1.add(Calendar.MONTH, 1);
}
return dates;
}
打印结果:
final List<Date> dates = mondaysFirst(1900, 1902);
final SimpleDateFormat sf = new SimpleDateFormat("dd MMM yyyy");
for (Date date: dates) {
System.out.println(sf.format(date));
}
我确信有一些Joda-Time魔法可以让它更短。