java吧 关注:1,271,345贴子:12,780,629
  • 7回复贴,共1

求助大神!!! 看我的哪里编错了。。。

只看楼主收藏回复

已知1601年1月1日是周一, 求输入的那一天是周几:
import java.util.*;
public class Problem4
{
public static void main(String[] args)
{
// Ask the user to input the date in month, day, year
Scanner console = new Scanner(System.in);
System.out.println("Please input the month");
int month = console.nextInt();
System.out.println("Please input the day");
int day = console.nextInt();
System.out.println("Please input the year");
int year = console.nextInt();
printDayofWeek(month, day, year);
}
// Calculate the sum of the days of perticular date
public static int dayCalculator(int month, int day, int year)
{
// Calculate the sum of the days of years
int totalDays = 0;
for (int i = 1; i < year; i++)
{
if (year%4 == 0)
{
totalDays = totalDays + 366;
}
else
{
totalDays = totalDays + 365;
}
}
// Calculate the sum of the days of remained months
for (int i = 1; i < month; i++)
{
if (i == 1||i == 3||i == 5||i == 7||i == 8||i == 10)
{
totalDays = totalDays + 31;
}
else if (i == 4||i == 6||i == 9||i == 11)
{
totalDays = totalDays + 30;
}
// If the year is a leap year, Feburary has 29 days
else if (i == 2 && year%4 == 0)
{
totalDays = totalDays + 29;
}
else
{
totalDays = totalDays + 28;
}
}
return totalDays = totalDays + day;
}
public static void printDayofWeek(int month, int day, int year)
{
// Call the static method to Calculate the days difference
int dayDifference = dayCalculator(month,day,year) - dayCalculator(1,1,1601);
// Use modulus to get the day of week
int dayOfWeek = dayDifference % 7;
if (dayOfWeek == 0)
{
System.out.println("This day is a Monday");
}
else if (dayOfWeek == 1)
{
System.out.println("This day is a Tuseday");
}
else if (dayOfWeek == 2)
{
System.out.println("This day is a Wednesday");
}
else if (dayOfWeek == 3)
{
System.out.println("This day is a Thursday");
}
else if (dayOfWeek == 4)
{
System.out.println("This day is a Friday");
}
else if (dayOfWeek == 5)
{
System.out.println("This day is a Saturday");
}
else
{
System.out.println("This day is a Sunday");
}
}
}
半天看不出来,求救。。


1楼2016-10-24 02:47回复
    没有编译错误,就是输出的星期不对


    2楼2016-10-24 02:47
    回复
      2025-07-27 16:01:15
      广告
      不感兴趣
      开通SVIP免广告
      学习java 加 QUN 4745-27227


      来自手机贴吧3楼2016-10-24 02:48
      回复
        自顶


        6楼2016-10-24 04:11
        回复
          学习java就加QUN : 202,250194


          来自iPhone客户端7楼2016-10-24 04:12
          回复
            我知道我哪错了。。
            1. 如果年份可以被100整除需要能被400整除才是闰年
            2. 我把forloop里面的i写成了year。。。


            9楼2016-10-24 05:11
            回复


              10楼2016-10-24 11:27
              回复