2015-04-07 41 views
-1

我有這種方法,允許用戶搜索帶有字符串的天數組 我錯誤地放置了我的catch異常,因爲我無法讓它工作。而sysout打印請輸入一天兩次,我不知道該把它放在哪裏。否則,如果方法書寫正確,方法將打印日期。Java array days

public void getDay() { 

    String days[] = {"", "Monday", "Tuesday", "Wednesday", 
     "Thursday", "Friday", "Saturday", "Sunday"}; 
    boolean isbol = true; 



    while (isbol) { 
     try { 
      System.out.println("Please enter a day: "); 
      day = in.nextLine(); 

      for (int i = 1; i < days.length; i++) { 

       if (day.equals(days[i])) { 
        System.out.println(i + 1); 
        isbol = false; 

       } 

      } 
     } catch (Exception e) { 
      System.out.println("Wrong input, please try again"); 
     } 

    } 

} 

回答

0

我認爲沒有必要使用try/catch,如果/ else足夠簡單,簡化。

String days[] = { "", "Monday", "Tuesday", "Wednesday", "Thursday", 
      "Friday", "Saturday", "Sunday" }; 
    boolean finish = false; // use a readable variable name 

    while (!finish) { 
     System.out.println("Please enter a day: "); 
     day = in.nextLine(); 
     for (int i = 1; i < days.length; i++) { 
      if (day.equalsIgnoreCase(days[i])) { // match upper and lower case 
       System.out.println("the index of day in the array is " + (i + 1)); 
       finish = true; 
       break; // stop the loop if found the day in array 
      } 
     } 
     if (!finish) 
      System.out.println("Can't find " + day 
        + " in the array, please try again!"); 
    } 
0

您不應該在這裏使用try/catch。你沒有拋出任何例外,你只是試圖評估一個條件。這就是爲什麼這個例外從未被調用。

我覺得這是你要的東西:

 //added this to get the program to compile on my machine 
     String day; 
     Scanner in = new Scanner(System.in); 

     String days[] = {"", "Monday", "Tuesday", "Wednesday", 
        "Thursday", "Friday", "Saturday", "Sunday"}; 
       boolean isbol = true; 



       while (isbol) { 
         System.out.println("Please enter a day: "); 
         day = in.nextLine(); 

         for (int i = 1; i < days.length; i++) { 

          if (day.equals(days[i])) { 
           //I assume you are trying to output the day here. 
           System.out.println(days[i]); 
           isbol = false; 

          } 


         } 
         //Conditional check to see if the user input a day 
         if(isbol){ 

           System.out.println("Wrong input, please try again"); 
         } 
       } 

} 

另外,請記住,這是設置方式,循環將只有當你鍵入一天像Monday打破。如果輸入monday,循環將繼續。

+0

好了,帶走了try/catch語句,但是當方法運行是這樣的第一輸出:請輸入天: 輸入錯誤,請重試 請輸入天: –

+0

你在輸入的時候該程序會問你一天嗎?我在這裏建立的方式是有效的。我已經在我的機器上測試過了。 – BlackHatSamurai

+0

這是我進入一天之前,否則它工作完美,但它打印這些樹線 –

0

我想你想趕上錯誤天數組中沒有匹配。由於沒有匹配時不會引發運行時異常,因此您的catch子句將永遠不會執行。 一個解決方案是添加一個If語句,當for循環耗盡時拋出異常。

public static void main(String[] args){ 
    Scanner input = new Scanner(System.in); 
    String days[] = {"", "Monday", "Tuesday", "Wednesday", 
      "Thursday", "Friday", "Saturday", "Sunday"}; 
    boolean isbol = true; 

    while (isbol) { 
     try { 
      System.out.println("Please enter a day: "); 
      String day = input.nextLine(); 

      for (int i = 1; i < days.length; i++) { 

       if (day.equalsIgnoreCase(days[i])) { 
        System.out.println(i + 1); 
        isbol = false; 
       } 
      } 
      if(isbol) 
       throw new Exception(); 
     } catch (Exception e) { 
      System.out.println("Wrong input, please try again"); 
     } 

    } 
    input.close(); 
} 
+0

這工作到:) –

0

我想你在System.in都盡顯在代碼的某個地方,這就是爲什麼你會得到「請輸入一個天:」消息的兩倍。另外,您不需要任何異常處理。您可以在下面找到代碼的修改版本。在方法內使用System.in並關閉它。

public int getDay() { 
    Scanner in = null; 
    try { 
     in = new Scanner(System.in); 
     String days[] = { "", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; 
     for (;;) { 
      System.out.println("Please enter a day: "); 
      String day = in.nextLine(); 
      for (int i = 1; i < days.length; i++) { 
       if (day.equalsIgnoreCase(days[i])) { 
        System.out.println(i+1); 
        return i + 1; 
       } 
      } 
     } 
    } finally { 
     in.close(); 
    } 
} 
+0

謝謝,工作 –