已知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");
}
}
}
半天看不出来,求救。。
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");
}
}
}
半天看不出来,求救。。