我已写完所有代码,但遗漏了一些东西。它的两个类一起工作以输出一周中的特定日期。我知道我做了一些愚蠢的错误,任何帮助都会受到赞赏
基本上,2个输出是正确的,2个是不正确的。两个不正确的是前一天和第二天,而不是得到正确的答案,我得到,比如,周一1或周二6:
import java.util.*;
public class DaysOfTheWeek
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
String currentDayOfTheWeek;
String dayOfTheWeek;
int dayToFind;
System.out.println("Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday");
System.out.print("Enter a day of the week: ");
currentDayOfTheWeek = console.nextLine();
Day newDay = new Day(currentDayOfTheWeek);
newDay.printTheDay();
dayOfTheWeek = newDay.getThePreviousDay(currentDayOfTheWeek);
System.out.println();
System.out.print("The PREVIOUS day from '" + currentDayOfTheWeek + "' ");
System.out.print("is '" + dayOfTheWeek + "' \n");
dayOfTheWeek = newDay.getTheNextDay(currentDayOfTheWeek);
System.out.println();
System.out.print("The NEXT day from '" + currentDayOfTheWeek + "' ");
System.out.print("is '" + dayOfTheWeek + "' \n");
System.out.println();
System.out.println("How many days in the future from '" + currentDayOfTheWeek +"' \n");
System.out.print("Do you want (0 - 31)?: ");
dayToFind = console.nextInt();
if (validateDay(dayToFind) == true)
{
dayOfTheWeek = newDay.getTheFutureDay(currentDayOfTheWeek, dayToFind);
System.out.println();
System.out.print(dayToFind + " days from '" + currentDayOfTheWeek + "' ");
System.out.print("is '" + dayOfTheWeek + "' \n");
}
else {
System.out.println();
System.out.println("Invalid Day '" + dayToFind + "'. Entered day must be between 0 and 31");
}
}
public static boolean validateDay(int dayToFind)
{
boolean validDay;
if ((dayToFind < 0) || (dayToFind > 31))
validDay = false;
else
validDay = true;
return validDay;
}
public class day {
}
}
对此课程进行了哪些调用:
public class Day
{
private String[] daysArray = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
private String dayOfTheWeek;
public Day()
{
dayOfTheWeek = "";
}
public Day(String whichDay)
{
setTheDay(whichDay);
}
public void setTheDay(String whichDay)
{
dayOfTheWeek = whichDay;
}
public void printTheDay()
{
System.out.println();
System.out.println("Day of the week entered is: " + dayOfTheWeek);
}
public String getTheFutureDay(String currentDayString, int futureDay)
{
int todayArrayPos = -1;
for (int i = 0; i < daysArray.length; i++)
{
if (daysArray[i].equalsIgnoreCase(currentDayString))
{
todayArrayPos = i;
break;
}
}
if (todayArrayPos == -1)
{
return "Invalid Day " + futureDay;
}
boolean bailOut = false;
int newFutureDay = 0;
int j = todayArrayPos;
while (newFutureDay != futureDay)
{
while (j < daysArray.length)
{
if (newFutureDay == futureDay)
{
bailOut = true;
break;
}
j += 1;
newFutureDay += 1;
}
if (bailOut == true)
{
break;
}
j = 0;
}
return daysArray[j];
}
public String getTheNextDay(String currentDayString)
{
String nextDayString = (dayOfTheWeek) + 1;
return nextDayString;
}
public String getThePreviousDay(String currentDayString)
{
String previousDayString = (dayOfTheWeek) + 6;
return previousDayString;
}
}
任何帮助将不胜感激!提前致谢
标记
答案 0 :(得分:0)
所以你的问题是你在第二天的方法中为一个字符串添加1。
public String getTheNextDay(String currentDayString)
{
String nextDayString = (dayOfTheWeek) + 1;
return nextDayString;
}
这将导致字符串连接 - 程序不明白当你要求它做“星期二”+ 1时你想要它说“星期三”。
你需要做一些与你未来的方法类似的事情:
int todayArrayPos = -1;
for (int i = 0; i < daysArray.length; i++)
{
if (daysArray[i].equalsIgnoreCase(currentDayString))
{
todayArrayPos = i;
break;
}
}
int nextDayIndex = i + 1;
//roll over to the first day of the week if the old day was the last day of the week
if (nextDayIndex == daysArray.length) {
nextDayIndex = 0;
}
return daysArray[nextDayIndex];
尝试前一天类似的事情。