I have a homework question that reads as such: Write a program that prompts the user to enter a date (in the form of three numbers: year month day), and use the getDays()
method to help count the number of days until New Year from the given date. Here are some sample runs:
- Please enter a date: 2018 12 20
There are 12 day(s) until New Year. - Please enter a date: 1980 1 5
There are 362 day(s) until New Year.
This is the code I have so far:
public class Lab9Question4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a date: ");
int year = input.nextInt();
int month = input.nextInt();
int days = input.nextInt();
input.close();
long diff = getDays(year, month) - days;
long days_diff = diff + 1;
System.out.println("There are " + days_diff + " day(s) until New Year.");
}
public static int getDays(int year, int month) {
int number_Of_DaysInMonth = 0;
switch (month) {
case 1:
number_Of_DaysInMonth = 31;
break;
case 2:
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
number_Of_DaysInMonth = 29;
} else {
number_Of_DaysInMonth = 28;
}
break;
case 3:
number_Of_DaysInMonth = 31;
break;
case 4:
number_Of_DaysInMonth = 30;
break;
case 5:
number_Of_DaysInMonth = 31;
break;
case 6:
number_Of_DaysInMonth = 30;
break;
case 7:
number_Of_DaysInMonth = 31;
break;
case 8:
number_Of_DaysInMonth = 31;
break;
case 9:
number_Of_DaysInMonth = 30;
break;
case 10:
number_Of_DaysInMonth = 31;
break;
case 11:
number_Of_DaysInMonth = 30;
break;
case 12:
number_Of_DaysInMonth = 31;
}
return number_Of_DaysInMonth;
}
}
I understand I need to use a loop somewhere in order to get the remaining amount of days after the user input and add them to the difference of dates to get the days until the new year, but I am not sure where to insert the loop and what should be inside it. Any help is appreciated :)
UPDATE: This code works perfectly now thanks to everyone who answered super quickly! New code is below for anyone else who might need it in the future:
public class Lab9Question4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a date: ");
int year = input.nextInt();
int month = input.nextInt();
int days = input.nextInt();
input.close();
int remainingDays = getDays(month, year) - days + 1;
for (int currentMonth = month; currentMonth <= 12; currentMonth++) {
remainingDays += getDays(year, currentMonth);
}
System.out.println("There are " + remainingDays + " day(s) until New Year.");
}
public static int getDays(int year, int month) {
int number_Of_DaysInMonth = 0;
switch (month) {
case 1:
number_Of_DaysInMonth = 31;
break;
case 2:
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
number_Of_DaysInMonth = 29;
} else {
number_Of_DaysInMonth = 28;
}
break;
case 3:
number_Of_DaysInMonth = 31;
break;
case 4:
number_Of_DaysInMonth = 30;
break;
case 5:
number_Of_DaysInMonth = 31;
break;
case 6:
number_Of_DaysInMonth = 30;
break;
case 7:
number_Of_DaysInMonth = 31;
break;
case 8:
number_Of_DaysInMonth = 31;
break;
case 9:
number_Of_DaysInMonth = 30;
break;
case 10:
number_Of_DaysInMonth = 31;
break;
case 11:
number_Of_DaysInMonth = 30;
break;
case 12:
number_Of_DaysInMonth = 31;
}
return number_Of_DaysInMonth;
}
}