Дни недели

Я пытаюсь создать программу, которая получает день от пользовательского ввода, а затем сообщает ему за день до и на следующий день. Пользователь также должен иметь возможность указать, сколько дней нужно добавить, и программа должна вывести этот день.

пример пользователь вводит 1 = понедельник, завтра = 2 вторник вчера был = 3 воскресенье

если пользователь говорит, что его понедельник (1) и добавляет 12 дней, выходной должен быть суббота (6)

Проблема в том, что всякий раз, когда "theWeekDay" больше 7, он ничего не выводит, потому что TheDay(); не имеет условия для чего-то большего, чем 7. Пожалуйста, помогите мне!

Спасибо большое!

import java.util.Scanner;
import java.util.Scanner;

public class Problem_3 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int theWeekDay;
        System.out.println("What Day Is It?");
        theWeekDay = input.nextInt();
        Days one = new Days(theWeekDay);
        System.out.println("Today It Is: ");
        one.TheDay(theWeekDay);
        System.out.println("Yesterday It Was: ");
        one.PreviousDay(theWeekDay);
        System.out.println("Tomorrow It Is: ");
        one.NextDay(theWeekDay);
        System.out.println("How Many Days To Add?");
        int x = input.nextInt();
        System.out.println("Now It Is: ");
        one.AddedDays(x);
    }
}

class Days {
    private int theWeekDay;

    public Days(int theWeekDay) {
        this.theWeekDay = theWeekDay;
    }

    public int getTheWeekDay() {
        return theWeekDay;
    }

    public void setTheWeekDay(int theWeekDay) {
        this.theWeekDay = theWeekDay;
    }

    public int TheDay(int theWeekDay) {
        // an arra days of week + then add days in it
        if (theWeekDay == 0) {
            theWeekDay = theWeekDay + 7;
        }

        if (theWeekDay == 1) {
            System.out.println("Monday");
        } else if (theWeekDay == 2) {
            System.out.println("Tuesday");
        } else if (theWeekDay == 3) {
            System.out.println("Wednsday");
        } else if (theWeekDay == 4) {
            System.out.println("Thursday");
        } else if (theWeekDay == 5) {
            System.out.println("Friday");
        } else if (theWeekDay == 6) {
            System.out.println("Saturday");
        } else if (theWeekDay == 7) {
            System.out.println("Sunday");
        }
        return theWeekDay;
    }

    public int PreviousDay(int theWeekDay) {
        theWeekDay = theWeekDay - 1;
        return TheDay(theWeekDay);
    }

    public int NextDay(int theWeekDay) {
        theWeekDay = theWeekDay + 1;
        if (theWeekDay > 7) {
            theWeekDay = 1;
        }
        return TheDay(theWeekDay);
    }

    public int AddedDays(int AddedDays) {
        getTheWeekDay();
        theWeekDay = theWeekDay + AddedDays;
        return TheDay(theWeekDay);
    }
}

3 ответа

Решение

Если вы хотите принять значение больше 7, как действительное, вам определенно следует использовать операцию по модулю. Что-то вроде этого:

if(theWeekDay > 7) {
    theWeekDay = theWeekDay % 7;
}

В противном случае вы должны бросить и исключение.

Как сказал user629735, используйте модуль по своей формуле.

public int AddedDays(int AddedDays) {
    getTheWeekDay();
    theWeekDay = (theWeekDay + AddedDays) % 7;
    return TheDay(theWeekDay);
}

Ваш if else должен охватывать все дела....

добавить еще после

else if(theWeekDay == 7){
        System.out.println("Sunday");
    }

что-то вроде:

if(theWeekDay == 1){
    System.out.println("Monday");
}else if(theWeekDay == 2){
    System.out.println("Tuesday");
}else if(theWeekDay == 3){
    System.out.println("Wednsday");
}else if(theWeekDay == 4){
    System.out.println("Thursday");
}else if(theWeekDay == 5){
    System.out.println("Friday");
}else if(theWeekDay == 6){
    System.out.println("Saturday");
}else if(theWeekDay == 7){
    System.out.println("Sunday");
}else{
    System.out.println("Invalid input");
}
return theWeekDay;
Другие вопросы по тегам