Java (Find the future date with if else statements)

后端 未结 6 1572
故里飘歌
故里飘歌 2020-12-22 14:39

I have a question that I can\'t figure it out , Thank you: Write a program that prompts the user to enter an integer for today\'s day of the week (Sunday is 0 ,Monday is 1 ,

6条回答
  •  抹茶落季
    2020-12-22 15:09

    package javaapplication2;
    
    import java.util.Scanner;
    public class JavaApplication2 {
    
    
        public static void main(String[] args) {
    
        int day, eday, fday;
            String str, str1;
            Scanner S = new Scanner(System.in);
            System.out.println("Enter today's day: ");
            day = S.nextInt();
            System.out.println("Enter the number of days elapsed since today: ");
            eday = S.nextInt();
            if (day == 0) {
                str = "Sunday";
                System.out.print("Today is " +str + " and ");
            }
            else if (day == 1) {
                str = "Monday";
                System.out.print("Today is " +str + " and ");
            }
            else if (day == 2) {
                str = "Tuesday";
                System.out.print("Today is " +str + " and ");
            }
            else if (day == 3) {
                str = "Wednesday";
                System.out.print("Today is " +str + " and ");
            }
            else if (day == 4) {
                str = "Thursday";
                System.out.print("Today is " +str + " and ");
            }
            else if (day == 5) {
                str = "Friday";
                System.out.print("Today is " +str + " and ");
            }
            else if (day == 6) {
                str = "Saturday";
                System.out.print("Today is " +str + " and ");
            }
    
           fday = day + eday;
    
           if (fday % 7 == 0) {
                str1 = "Sunday";
                System.out.print("Future day is " +str1);
            }
            else if (fday % 7 == 1) {
                str1 = "Monday";
                System.out.print("Future day is " +str1);
            }
            else if (fday % 7 == 2) {
                str1 = "Tuesday";
                System.out.print("Future day is " +str1);
            }
            else if (fday % 7 == 3) {
                str1 = "Wednesday";
                System.out.print("Future day is " +str1);
            }
            else if (fday % 7 == 4) {
                str1 = "Thursday";
                System.out.print("Future day is " +str1);
            }
            else if (fday % 7 == 5) {
                str1 = "Friday";
                System.out.print("Future day is " +str1);
            }
            else if (fday % 7 == 6) {
                str1 = "Saturday";
                System.out.print("Future day is " +str1);
            }
    
        }
    

提交回复
热议问题