我正在完成一项任务,我必须创建一个需要做一系列事情的课程日。我已经弄明白了,大部分都很简单,但我遇到了一个问题。其中一种方法可以让您将日期设置为您想要的一周中的任何一天。例如,程序从设置为Fri的日期开始,我应该能够输入Mon然后告诉它输出,它应该输出Mon.由于某种原因,它没有更新当天,我无法弄明白。有谁看到我在这里缺少的东西?
The purpose of this program is to create a class Day that will perform
multiple tasks regarding the day of the week. */
import java.util.*;
public class DayProgram
{
static Scanner keyboard = new Scanner(System.in);
public static class Day
{
String[] weekDays = {"Sun" , "Mon", "Tues", "Weds", "Thurs", "Fri", "Sat"};
int setDay = 0;
int returnDay = 0;
public Day(String today)
{
for (int i = 0; i < weekDays.length; i++)
{
if (today.equals(weekDays[i]))
{
setDay = i;
return;
}
}
System.out.println("That is not a valid day.");
}
public void printDay()
{
if (setDay <= weekDays.length)
{
System.out.println("Today is " + weekDays[setDay]);
}
else System.out.println("Day is not valid");
}
public String setDay()
{
String newDay = keyboard.next();
return newDay;
}
public String tomorrow()
{
return weekDays[(setDay + 1)%weekDays.length];
}
public String yesterday()
{
return weekDays[(setDay - 1)%weekDays.length];
}
public String findDay(int x)
{
return weekDays[(setDay + x)%weekDays.length];
}
public String returnDay()
{
return weekDays[setDay];
}
}
public static void main(String[] args)
{
Day day = new Day("Fri");
day.printDay();
System.out.println("Please change the day of the week.");
day.setDay();
day.printDay();
System.out.println(day.returnDay());
System.out.println(day.tomorrow());
System.out.println(day.yesterday());
System.out.println("Please enter number of days and I will tell you what day of the week it will be.");
System.out.println(day.findDay(keyboard.nextInt()));
}
}
答案 0 :(得分:1)
setDay()
不会修改变量setDay
。只需改变
newDay = input.next()
到setDay = input.next()
对对象进行持久更改。
答案 1 :(得分:0)
public String setDay()
{
String newDay = keyboard.next();
return newDay;
}
您初始化了本地变量'newDay',但不将其分配给'setDay'。
你返回那个newDay,但没有人抓住结果。
答案 2 :(得分:0)
更改时间:
System.out.println("Please change the day of the week.");
day.setDay();
day.printDay();
day.setDay();
要:
day.Day(day.setDay());
我不知道您是否可以将此day.setDay
功能设置为此功能。如果它不起作用,只需使用额外的变量。
顺便说一下,我不确定这是否正确我只是用你的东西为自己学习这个用户定义的方法/类的东西。