2016-04-05 117 views
0

我寫了所有的代碼,但我錯過了一些東西。這兩個班級一起工作來輸出一週中的特定日子。我知道我正在做一些愚蠢的錯誤,任何幫助,將不勝感激JAVA:返回不正確的答案

基本上,2輸出是正確的,2是不正確的。這兩個是不正確的前一天和第二天,而不是得到正確的答案,我得到,比如說,Monday1或Tuesday6:

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

您絕對需要提供更具描述性的問題陳述。就目前而言,我們知道您的代碼會在一週中的幾天內產生,但我們並不知道它具體做了什麼以及它應該生成的日期。 – nhouser9

+0

基本上它將一週中的幾天加載到數組中。要求用戶選擇開始日期,然後返回前一天,第二天輸入的日期,然後要求在0到31天之間添加以返回正確答案。它現在返回正確的開始日期和添加X天后的正確日期,只是沒有正確地返回前幾天和下一天,而不是將數組移動到下一個元素或後面的6個元素,它返回day1或day6。這對我瞭解我的問題更有幫助嗎? – markus242005

+0

是的非常有幫助,現在看到我的答案,你已澄清 – nhouser9

回答

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]; 

嘗試爲您的前一天類似的東西爲好。