我有一个最终项目发布在下面。我有程序正确编译,但我没有得到正确的结果。无论我为用户输入的是哪一天,它都会在周一作为第二天返回,而周日则作为前一天返回。对于当天添加天数的部分,似乎它设置了日期并且在星期日停留,因此星期一增加8返回,星期六增加6返回等等。这与用户输入日不同。
在询问我的导师错误之后,我收到了以下回复:"我看到的问题是addDays返回一个未被捕获的int。你添加到日期,并返回一天,但不做任何事情。"我不确定从哪里开始,请帮忙!
这是项目:
设计并实现在课程中实现星期几的课程日。上课日应该存储一天,例如太阳周日。该程序应该能够对Day类型的对象执行以下操作:
一个。设定一天。
B中。打印一天。
℃。回归这一天。
d。第二天回来。
电子。返回前一天。
F。通过在当天添加特定日期来计算并返回当天。例如,如果当天是星期一,我们添加四天,则返回的日期是星期五。同样,如果今天是星期二,我们加13天,则返回的日期是星期一。
-G。添加适当的构造函数。
小时。编写方法的定义以实现类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);
}
}
答案 0 :(得分: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());
答案 1 :(得分:0)
我认为字符串“day”变量未使用,并且没有更改Day(d)实例的状态。
String day = console.nextLine();
System.out.println("You entered: " + day);
如果您希望应用更改,可能需要添加以下代码。
d.setDay(Integer.parseInt(day));