Java (Find the future date with if else statements)

后端 未结 6 1552
故里飘歌
故里飘歌 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条回答
  •  梦毁少年i
    2020-12-22 15:15

    You can do it better by using an array to store the the day names.

    String[] dayNames = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    

    Now you can use the user input as the index

    int nameIndex = //... get input
    //validate input
    //dayNames[nameIndex] is the day of the week
    

    Now get the input for number of days to add

    int numDays = //...get input
    

    Then just add that many days to compute the index for future day of week

    int futureNameIndex = nameIndex; //start with entered day of week index
    for(int i=0; i

    I think you will find that one easier to understand. Finally

    //dayNames[futureNameIndex] is the future day of week.
    

提交回复
热议问题