一周中的日子

时间:2016-11-16 22:46:25

标签: java class dayofweek

我正在尝试创建一个程序,从用户输入获取当天,然后告诉他们前一天和后一天。用户还应该能够输入添加的天数,程序应该输出当天

示例用户输入1 =星期一,明天是= 2星期二星期二是= 3星期日

如果用户说出星期一(1)并且增加12天,则输出应该是星期六(6)

问题是每当“周日”和“#34;大于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 个答案:

答案 0 :(得分:0)

你的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;

答案 1 :(得分:0)

如果你想假设值7大于有效,你绝对应该使用模运算。像这样:

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

否则你应该抛出异常。

答案 2 :(得分:0)

正如user629735所说,在你的公式中使用modulo。

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