【问题标题】:Project to add and return days添加和返回天数的项目
【发布时间】:2015-05-04 22:29:55
【问题描述】:

我有一个最终项目发布在下面。我有程序可以正确编译,但我没有得到正确的结果。无论我为用户输入哪一天,它都会返回星期一作为第二天和星期日作为前一天。对于将日期添加到当前日期的部分,它似乎已设置日期并停留在星期日,因此星期一添加 8 个返回,星期六添加 6 个返回,等等。这与用户输入的日期无关。

在向我的导师询问出了什么问题后,我得到了以下答复:“我看到的问题是 addDays 返回一个未捕获的 int。您添加天数,然后返回一天,但什么也不做。”我不知道从哪里开始,请帮助!

这里是项目:

设计并实现在程序中实现星期几的 Day 类。类 Day 应该存储日期,例如 Sun 代表星期日。该程序应该能够对 Day 类型的对象执行以下操作:

A.确定日期。

B.打印当天。

C.返回当天。

D.次日返回。

E.返回前一天。

F。通过将某些天添加到当前日期来计算并返回日期。例如,如果当前日期是星期一,我们添加四天,则要返回的日期是星期五。同样,如果今天是星期二,我们加上 13 天,则返回的日期是星期一。

G.添加适当的构造函数。

H.编写实现类 Day 操作的方法的定义,如 A 到 G 中定义的那样。

我。编写一个程序来测试上课日的各种操作。 我在下面发布了我的整个代码:

这是我的代码:

//**************************************************************************
//  Author: Eric Miller
//  IT-145 Module Eight, Final Project
//  February 28, 2015
//
//  This program will provide a variety of functions on a day of the week
//  entered by a user.  Among these will be the function to return the day,
//  return the next day, the previous day, and also calculate future days by
//  adding a number to the current day.
//**************************************************************************

import java.util.*;

public class Day 

{
    //Day Constants
   private static final int SUNDAY = 0;
    private static final int MONDAY = 1;
    private static final int TUESDAY = 2;
    private static final int WEDNESDAY = 3;
    private static final int THURSDAY = 4;
    private static final int FRIDAY = 5;
    private static final int SATURDAY = 6;

    //Stores the day of the week.
   private int day;

    //Sets the day.
   public void setDay(int day) 
   {
        this.day = day;
    }

    //Prints the day.
    public void print() 
   {
        System.out.println(this.toString());
    }

   //Converts int to string
   public String toString() 
   {
        switch (this.day) 
      {
        case SUNDAY:
            return "Sunday";
        case MONDAY:
            return "Monday";
        case TUESDAY:
            return "Tuesday";
        case WEDNESDAY:
            return "Wednesday";
        case THURSDAY:
            return "Thursday";
        case FRIDAY:
            return "Friday";
        case SATURDAY:
            return "Saturday";
        }
        return "";
    }

    //Returns the day.
    public int getDay() 
   {
        return day;
    }

    //Returns the next day.
    public int getNextDay() 
   {
        return (day + 1) % 7;
    }

    //Returns the previous day.
    public int getPrevDay() 
   {
        return (day - 1) % 7;
    }

    //Calculates future day by adding a number of days to current day.
    public int addDays(int days) 
   {
        return (day + days) % 7;
    }

    //Constructors.
    public Day() 
   {
        this.day = day;
    }

    public Day(int day) 
   {
        this.day = day;
    }

    //Main method. 

   static Scanner console = new Scanner(System.in);

   public static void main(String[] args) 

{
      Day d = new Day();

        System.out.println("Enter a day of the week: ");
        String day = console.nextLine();
        System.out.println("You entered: " + day);

        System.out.print("The day after the day you entered is: ");
      d.setDay(d.getNextDay());
      System.out.println(d);

        System.out.print("The day before the day you entered is: ");
        d.setDay(d.getPrevDay());
      System.out.println(d);

        System.out.print("Enter a number of days to add to the current day: ");
        int days = console.nextInt();
      d.setDay(d.addDays(days));
      System.out.print("The day you entered plus the added days is: ");
        System.out.println(d);

    }
}

【问题讨论】:

  • 欢迎来到 SO。甚至不确定从哪里开始,但首先,您正在将字符串读入 day 变量,该变量隐藏了名为 day 的 int 数据成员,并且也不对其执行任何操作。这需要做很多工作,而且 SO 可能不是这种所需辅导水平的正确论坛。建议阅读Java Tutorials,但如果这是您的最终结果,可能为时已晚。

标签: java


【解决方案1】:

您永远不会使用用户输入的值作为Day 的种子,因此它始终默认为SUNDAY (0)

    System.out.println("Enter a day of the week: ");
    String day = console.nextLine();
    System.out.println("You entered: " + day);
    d.setDay(Integer.parseInt(day));

这假定用户要输入 0-6

现在,这将打印第二天,但不会打印前一天(它将打印用户输入的日期),因为您将 day 重置为第二天的值...

d.setDay(d.getNextDay());

最好只打印结果,但您没有方法可以做到这一点,因此...

System.out.println(new Day(d.getNextDay());

System.out.println(new Day(d.getPrevDay());

【讨论】:

    【解决方案2】:

    我认为字符串“day”变量没有被使用并且没有改变你 Day (d) 实例的状态。

    String day = console.nextLine();
    System.out.println("You entered: " + day);
    

    如果您希望应用更改,您可能需要添加以下代码。

    d.setDay(Integer.parseInt(day));
    

    【讨论】:

      猜你喜欢
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      • 2014-11-17
      • 2012-02-09
      • 1970-01-01
      • 2017-07-17
      相关资源
      最近更新 更多