Write a program that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1, …, and Saturday is 6). Also prompt the user to enter the number of days after today for a future day and display the future day of the week.
Solution:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int today, elapsedDays;
int daysToAdd, dayToFind;
System.out.print("Enter today's day:");
today = s.nextInt();
System.out.print("Enter the number of days elapsed since today:");
elapsedDays = s.nextInt();
daysToAdd = elapsedDays % 7;
dayToFind = today + daysToAdd;
if(today == 0) {
System.out.print("To day is Sunday");
} else if(today == 1) {
System.out.print("To day is Monday");
} else if(today == 2) {
System.out.print("To day is Tuesday");
} else if(today == 3) {
System.out.print("To day is Wednesday");
} else if(today == 4) {
System.out.print("To day is Thursday");
} else if(today == 5) {
System.out.print("To day is Friday");
} else if(today == 6) {
System.out.print("To day is Saturday");
}
if(dayToFind == 0) {
System.out.print(" and the future day is Sunday.");
} else if(dayToFind == 1) {
System.out.print(" and the future day is Monday.");
} else if(dayToFind == 2) {
System.out.print(" and the future day is Tuesday.");
} else if(dayToFind == 3) {
System.out.print(" and the future day is Wednesday.");
} else if(dayToFind == 4) {
System.out.print(" and the future day is Thursday.");
} else if(dayToFind == 5) {
System.out.print(" and the future day is Friday.");
} else if(dayToFind == 6) {
System.out.print(" and the future day is Saturday.");
}
}
No comments:
Post a Comment