【发布时间】: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