I am trying to solve this problem, in which you enter a number 1-7 which corresponds to the days of the week, (Monday-Sunday) then you add a number between (0-20) and the program returns the day of the week. My current solution works except for 'Sunday'.
public static void main(String[] args) {
int day;
int num;
int newDay;
String [] days = new String[] {"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"};
System.out.println("Enter the day and the number to add");
Scanner scnr = new Scanner(System.in);
day = scnr.nextInt();
num = scnr.nextInt();
newDay = (num + day) % 7;
System.out.println(newDay);
System.out.println("The new day is " + days[newDay - 1] );
}
As you can see, if the program were to hit "Sunday," the array would go out of bounds. I could easily just say "if newDay = 0, set newDay = 7" but I want to see if there's a different solution.
Thanks for the help!