2016-07-23 28 views
0

我正在使用JOptionPane天左右Java程序工作。我不是在談論日期(2016年10月14日等),但天:Sunday, Monday陣列櫃不與Java

我的問題是功能,添加特定天數的當天(例如今天是星期二然後添加5天后)。

我使用陣列訪問/指用於顯示和輸出的目的的特定一天。

我引用星期日至星期六,與分別0-6數組索引。

的問題是,假設當天是週六,如果用戶附加的3天進它的程序崩潰。

我相信這是因爲崩潰,因爲週六位於索引6和算法試圖訪問第九指數,它不存在。由於Java是一種安全的語言,它不會顯示'null',它決定崩潰。

什麼是應該在這種情況下發生的是,從星期六,程序將顯示週二,而不是:

異常線程 「main」 java.lang.ArrayIndexOutOfBoundsException:9

我使用Netbeans作爲我的IDE,我自學java,因爲我的大學教我C++,visual basic,c#爲我當年和課程。如果您想詳細信息,然後,

這裏的主類的源代碼:

package weekdaysprogram; 

import java.util.*; 

public class weekdayParametersProgramMain 
{ 

    static Scanner console = new Scanner (System.in); 

    public static void main(String[] args) 
    { 
     weekdayParametersProgram firstObject = new weekdayParametersProgram(); 
     firstObject.inputMainMenu(); 
    } 
} 

下面是第二類的源代碼:

package weekdaysprogram; 

import javax.swing.JOptionPane; 

public class weekdayParametersProgram 
{ 
    int currentDay; //variable used for referencing a certain day 

    String[] day = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; //Array of days 

    public void inputMainMenu() //Main menu function 
    { 
     int choice; 

     String inputStr; 
     String mainMenuStr = "Day Class\n" 
       + "Enter choice: \n" 
       + "1. Set Day \n" 
       + "2. Print Day \n" 
       + "3. Print Next Day \n" 
       + "4. Print Previous Day \n" 
       + "5. Calculate Day \n" 
       + "6. Exit"; 

     inputStr = JOptionPane.showInputDialog(mainMenuStr); 
     choice = Integer.parseInt(inputStr); 

     switch (choice) 
     { 
      case 1: 
       setDay(); 
       break; 
      case 2: 
       printDay(); 
       break; 
      case 3: 
       printNextDay(); 
       break; 
      case 4: 
       printPreviousDay(); 
       break; 
      case 5: 
       calculateDay(); 
       break; 
      case 6: 
       exit(); 
       break; 
      default: 
       JOptionPane.showMessageDialog(null, "Error Try Again", "Error", JOptionPane.ERROR_MESSAGE); 
       inputMainMenu(); 
       break; 
     } 
    } 


    public void calculateDay() //the 5th function I'm getting an error at aka the culprit of my error 
    { 
     String message = "Current Day: " + currentDay + " - " + day[currentDay] + "\n" 
       + "Please enter the amount of days to be added: "; 

     int tempNum1 = currentDay; 
     String tempNum2 = JOptionPane.showInputDialog(message); 
     int tempNum3 = Integer.parseInt(tempNum2); 

     for (int count=0; count <= tempNum3; count++) 
     { 
      if (count == 7) tempNum1 = 0; 
      else if (count==0) continue; //this is a patch, since there would be a confusion if the user inputted 1 for the addtional day 
      else tempNum1++; 
     } 

     String nextMessage = day[currentDay] + " + " + tempNum3 + " days = " + day[tempNum1]; 

     JOptionPane.showMessageDialog(null, nextMessage, "New Day", JOptionPane.INFORMATION_MESSAGE); 

     inputMainMenu(); 
    } 

    public void setDay() //1st function for set day 
    { 

     String inputStr; 

     String message = "Enter day index: \n" 
       + "0 = " + day[0] + "\n" 
       + "1 = " + day[1] + "\n" 
       + "2 = " + day[2] + "\n" 
       + "3 = " + day[3] + "\n" 
       + "4 = " + day[4] + "\n" 
       + "5 = " + day[5] + "\n" 
       + "6 = " + day[6] + "\n"; 
     inputStr = JOptionPane.showInputDialog(message); 

     switch (inputStr) 
     { 
      case "0": 
       currentDay = 0; 
       break; 
      case "1": 
       currentDay = 1; 
       break; 
      case "2": 
       currentDay = 2; 
       break; 
      case "3": 
       currentDay = 3; 
       break; 
      case "4": 
       currentDay = 4; 
       break; 
      case "5": 
       currentDay = 5; 
       break; 
      case "6": 
       currentDay = 6; 
       break; 
      default: 
       JOptionPane.showMessageDialog(null, "Please try again.", "Error", JOptionPane.ERROR_MESSAGE); 
       setDay(); 
       break; 
     } 

     inputMainMenu(); 
    } 

    public void printDay() //2nd function for printing out the current day 
    { 
     JOptionPane.showMessageDialog(null, day[currentDay], "Current Day", JOptionPane.INFORMATION_MESSAGE); 

     inputMainMenu(); 
    } 

    public void printNextDay() //3rd function for printing out the next day 
    { 
     int tempNum = currentDay; 

     if (tempNum == 6) tempNum = 0; 
     else tempNum += 1; 

     JOptionPane.showMessageDialog(null, day[tempNum], "Next Day", JOptionPane.INFORMATION_MESSAGE); 

     inputMainMenu(); 
    } 

    public void printPreviousDay() //4th function for printing out the previous day 
    { 
     int tempNum = currentDay; 

     if (tempNum == 0) tempNum = 6; 
     else tempNum -= 1; 

     JOptionPane.showMessageDialog(null, day[tempNum], "Previous Day", JOptionPane.INFORMATION_MESSAGE); 

     inputMainMenu(); 
    } 

    public void exit() 
    { 
     System.exit(0); 
    } 
} 

回答

3

而不是

for (int count=0; count <= tempNum3; count++) 
    { 
     if (count == 7) tempNum1 = 0; 
     else if (count==0) continue; //this is a patch, since there would be a confusion if the user inputted 1 for the addtional day 
     else tempNum1++; 
    } 

你可以使用

tempNum1 = (currentDay + tempNum3) % 7; 

%是模運算符,所以結果將總是6.介於0和

+1

做@哈羅德另外,你可以善用你的班級名稱嗎?這在Java中是基本的東西。 – ha9u63ar

0

calculateDay()方法的執行可能會失敗一些輸入。

  • 如果用戶輸入 「6」,tempNum3將是整數6.
  • 在for循環中,將count在0開始和循環,直到tempNum3,這是6
  • 對於值從1到6計數,tempNum1將每次遞增。
  • tempNum1currentDay(這本來也是6)和tempNum1初始化,將有大於6的值,從而導致IndexOutOfBoundsException字符串nextMessage生成時。

編輯:洛里斯的回答還提供了一個解決方案(使用模運算符),我建議你每次你改變days排列的指標。 (小心,雖然負值)

0

的問題是在calculateDay()方法

通過線讓你的鐵餅代碼行。

假設currentDay=5進入calculateDay()方法之前

現在仔細閱讀註釋代碼

String message = "Current Day: " + currentDay + " - " + day[currentDay] + "\n" 
      + "Please enter the amount of days to be added: ";//here currentDay =6 

    int tempNum1 = currentDay;//tempNum1=6 
    String tempNum2 = JOptionPane.showInputDialog(message);//suppose input "3" 
    int tempNum3 = Integer.parseInt(tempNum2);//tempNum3 = 3 

    for (int count=0; count <= tempNum3; count++) 
    { 
     if (count == 7) tempNum1 = 0; 
     else if (count==0) continue; 
     else tempNum1++;//for count value 1,2,3 tempNum1 will increase so it will be 7, 8 finally 9. So tempNum1=9 
    } 

    String nextMessage = day[currentDay] + " + " + tempNum3 + " days = " + day[tempNum1];//here tempNum1=9 So ArrayIndexOutOfBound occurs 

內要避免這種錯誤添加另一個檢查就像你在你的其他方法

JOptionPane.showMessageDialog(null, nextMessage, "New Day", JOptionPane.INFORMATION_MESSAGE); 


     for (int count=0; count <= tempNum3; count++) 
     { 
      if (count == 7) tempNum1 = 0; 
      else if (count==0) continue; 
      else { 
       tempNum1++ 

       if(tempNum1=7) tempNum=0; 
      } 
     }