有没有人能够用java中的数学指向我正确的方向?

时间:2015-02-25 00:33:56

标签: java

我正在尝试找出一个问题,我必须创建一个程序,该程序将调用一周中的几天,打印出选定日期之前和之后的日期,以及返回添加的日期,因此如果用户选择周日,该计划将在周六作为前一天返回,周一作为第二天返回,并将第x天数量添加到控制台中。我完全迷失了,可以使用任何建议...到目前为止我的代码是:

 import java.util.Scanner;

 public final class MurphyW8Day {
    private int murphingDay;
    private String namedDay;

    @Override
    public String toString() {
        return (namedDay);
    }
    public void setDay(int murphingDay) {
        if (murphingDay == 0)
            namedDay = "Sun";
        if (murphingDay == 1)
            namedDay = "Mon";
        if (murphingDay == 2)
            namedDay = "Tue";
        if (murphingDay == 3)
            namedDay = "Wed";
        if (murphingDay == 4)
            namedDay = "Thur";
        if (murphingDay == 5)
            namedDay = "Fri";
        if (murphingDay == 6)
            namedDay = "Sat";
    }
    public MurphyW8Day setNameDay(String murphingDay) {
        murphingDay = namedDay;
        return this;
    }
    public void printDay() {
        System.out.println(namedDay);
    }
    //I believe my issue is somewhere between here :

    public void previousDay() {
        murphingDay = (murphingDay - 1);
        setDay(murphingDay);
        printDay();
    }
    public void nextDay() {
        murphingDay = (murphingDay + 1) % 7;
        setDay(murphingDay);
        printDay();
    }

    //And Here:

    public void calculateDay() {
        int calc;
        int dayAdd;

        Scanner scanner = new Scanner(System. in );
        System.out.println("Enter number of days to add: ");
        calc = scanner.nextInt();

        dayAdd = murphingDay + (calc);

        murphingDay = dayAdd % 7;

        setDay(murphingDay);
        printDay();
    }
    public MurphyW8Day() {
        setDay(0);
    }
    public MurphyW8Day(int murphingDay) {
        setDay(murphingDay);
    }
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("What is your initial day?\nSunday\t\t=\t0\nMonday\t\t=\t1\nTuesday\t\t=\t2\nWednesday\t=\t3\nThursday\t=\t4\nFriday\t\t=\t5\nSaturday\t=\t6");
        System.out.print("Enter the number:");
        Scanner sc = new Scanner(System. in );
        int x = sc.nextInt();
        MurphyW8Day myDay = new MurphyW8Day(x);
        System.out.println("");

        System.out.print("The day of the week is: ");
        myDay.printDay();
        System.out.println();

        System.out.print("The previous day is: ");
        myDay.previousDay();

        System.out.println();

        System.out.print("The next day is: ");
        myDay.nextDay();

        System.out.println();

        myDay.calculateDay();
        System.out.println();
    }
 }

控制台中的输出是:

run:
What is your initial day?
Sunday      =   0
Monday      =   1
Tuesday     =   2
Wednesday   =   3
Thursday    =   4
Friday      =   5
Saturday    =   6
Enter the number:5
The day of the week is: Fri
The previous day is: Fri
The next day is: Sun
Enter number of days to add: 
5
Fri
BUILD SUCCESSFUL (total time: 6 seconds)

我无法弄清楚我错过了什么,正在通过我的书和oracle网站...请详细说明我只想完成这个程序,这样我就可以在学校度过一个非常值得的假期一个星期。洛尔

提前感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

以下是一些建议:

  1. 您的班级MurphyW8Day有两个等效字段,可以直接从另一个字段计算出来。我会删除其中一个,并添加一个方法来根据需要进行计算。我的偏好是删除namedDay,因为从日期编号中计算日期名称非常简单,事实上,您在setDay中几乎拥有所需的代码。
  2. 继续(1)后,您有两种设置方法,setDaysetNamedDay。其中一个可以删除,我的建议是删除与(1)中删除的字段相对应的那个。
  3. 最后,确保剩下的setter(例如setDay)设置类中的字段。目前还没有,我认为这是您当前问题的主要来源。
  4. 实际数学对我来说是正确的。

    另外,您可能需要考虑从输入和打印中分离计算,以便您可以单独使用它们。