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) and the number of days after today for a future day and display the future day of the week.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> */<br /><br />import java.util.Scanner;<br /><br />public class Ex03_05 {<br /> public static void main(String[] args) {<br /> String today;<br /> String futureday;<br /> <br /> Scanner input = new Scanner(System.in);<br /> <br /> System.out.print("Enter today's day: ");<br /> int day = input.nextInt();<br /> System.out.print("Enter the number of days elapsed since today: ");<br /> int numDays = input.nextInt();<br /> <br /> if (day == 0)<br /> today = "Sunday";<br /> else if (day == 1)<br /> today = "Monday";<br /> else if (day == 2)<br /> today = "Tuesday";<br /> else if (day == 3)<br /> today = "Wednesday";<br /> else if (day == 4)<br /> today = "Thursday";<br /> else if (day == 5)<br /> today = "Friday";<br /> else<br /> today = "Saturday";<br /> <br /> int calcFutureday = (day + numDays) % 7;<br /> <br /> if (calcFutureday == 0)<br /> futureday = "Sunday";<br /> else if (calcFutureday == 1)<br /> futureday = "Monday";<br /> else if (calcFutureday == 2)<br /> futureday = "Tuesday";<br /> else if (calcFutureday == 3)<br /> futureday = "Wednesday";<br /> else if (calcFutureday == 4)<br /> futureday = "Thursday";<br /> else if (calcFutureday == 5)<br /> futureday = "Friday";<br /> else<br /> futureday = "Saturday";<br /> <br /> System.out.println("Today is " + today + " and the future day is " + <br /> futureday);<br /> }<br />}<br /> |
Sample run |
Click here to see other solutions to Introduction to Java Programming.