2014-11-08 100 views
0

我正在通過命令行創建一個用戶界面,該用戶界面將要求從1到4的選項,並且我想要進行錯誤檢查。只允許1到4之間的整數。這是迄今爲止的代碼。我想方法返回userInput整數到另一個方法,它會做一些東西。try catch塊中的變量範圍

package contactmanager; 

import java.util.InputMismatchException; 
import java.util.Scanner; 

/** 
* 
* @author andyjohnson 
*/ 
public class UserInterface { 

    public static Integer GetInput() { 
     Scanner in = new Scanner(System.in); 
     //Integer userInput; 
     System.out.println("Welcome to the contact manager\nMake a selection below:"); 
     System.out.println("1)Display Contacts\n2)Add new business contact\n3)Add new personal contact\n4)Quit"); 
     try { 
      Integer userInput = in.nextInt(); 
      if (userInput < 1 || userInput > 4) { 
       System.out.println("Please enter a valid selection"); 
       UserInterface.GetInput(); 
      } 

     } catch (InputMismatchException e) { 
      e.getMessage(); 
      System.out.println("Please enter a valid selection"); 
      UserInterface.GetInput(); 
     } 

     return userInput; 

    } 

} 

我的return語句在IDE中被加下劃線,並告訴我它沒有被初始化。我想全局初始化它,但允許try語句改變它的值。我試過this.userInput = userInput,但我無法弄清楚我的範圍被破壞了。我如何給try塊的全局範圍?我是新來的Java,所以任何東西都有幫助。謝謝!

回答

0

你可以只聲明userInput變量try-catch塊外:

package contactmanager; 

import java.util.InputMismatchException; 
import java.util.Scanner; 

public class UserInterface { 

    public static Integer GetInput() { 
     Scanner in = new Scanner(System.in); 
     System.out.println("Welcome to the contact manager\nMake a selection below:"); 
     System.out.println("1)Display Contacts\n2)Add new business contact\n3)Add new personal contact\n4)Quit"); 
     Integer userInput = null; 
     try { 
      userInput = in.nextInt(); 
      if (userInput < 1 || userInput > 4) { 
       System.out.println("Please enter a valid selection"); 
       UserInterface.GetInput(); 
      } 

     } catch (InputMismatchException e) { 
      e.getMessage(); 
      System.out.println("Please enter a valid selection"); 
      UserInterface.GetInput(); 
     } 

     return userInput; 

    } 

} 
0

try-catch塊的涵蓋範圍是不同的,只是寫行內代碼的方法的其餘部分。

我想你所遇到的問題是在這條線,其中變量userInput首先在try-catch塊內部聲明:

Integer userInput = in.nextInt(); 

之所以這樣,是一個問題:

考慮一下,如果try-catch阻止失敗。然後會返回什麼?變量userInput還沒有定義,所以Java不知道該返回什麼。

修復相對簡單。您想要將其從try-catch塊中移出,就像這樣。這應該擺脫您的return錯誤。我注意到你評論了這個改變。爲什麼?

但我有一個額外的建議。你爲什麼打電話給UserInterface.GetInput()?爲什麼不讓方法接受有效輸入的參數,並且在數據格式不正確時根本不調用它?你用它嗎?這將消除真正的全球範圍變量的需求。

由於如何編寫此方法,它必須返回某種類型的Integer,除非您編寫該方法,否則該方法會引發在下游某個位置捕獲的異常。

我試圖做一些修正,我認爲將最有意義:

Scanner in = new Scanner(System.in); 
Integer userInput; // Integer representation of user input 

try { 
    Integer a = in.nextInt(); // if a can be assigned to an Integer this line work 
    if (a < 1 || a > 4) { 
     // called if the input can be assigned to an Integer and is within the range 
     userInput = a; 
    } 

} catch (InputMismatchException e) { 
    // otherwise the catch block is called 
    System.out.println("Please enter a valid selection"); 
} 

return userInput; 

也許你想打電話UserInterface.GetInput()範圍檢查裏面?希望這可以幫助!

編輯:使用識別標記,而不是回顧的方法

Scanner input = new Scanner(System.in); 
System.out.println("Please enter a valid Integer. When done type DONE "); 

// this method will keep cycling until the String DONE is typed 
// you could make this condition whatever you want 
while (!input.hasNext("DONE")) { 

    String a = input.next(); // gets the next item from the Scanner 

    try { 
     Integer b = Integer.parseInt(a); // tries to 'cast' the String to an Integer 

     if (b < 1 || b > 4) { 
      System.out.println("Input is valid!"); 
     } else { 
      System.out.println("Input is invalid!"); 
     } 

    } catch (NumberFormatException e) { 
     System.out.println("Please enter a valid selection!"); 
    } 
} 
+0

我需要爲每個規範再次調用UserInterface.GetInput()。如果輸入驗證失敗,程序需要重新輸入菜單。我試過這個建議,但它似乎沒有驗證輸入的整數,如果它是0或5.輸入像foo這樣的字符串給了我在catch語句中的代碼。如果嘗試失敗驗證或者catch執行什麼是再次調用該菜單方法的最佳方式?規範說這應該給用戶輸入驗證失敗後的菜單。我想讓UserInterface方法是靜態的,所以它不需要實例化。 – 2014-11-08 21:06:02

+0

我不知道你正在查看的'規範'中列出了什麼條件。我測試了這個方法,它似乎適用於我。我不明白'UserInterface.GetInput()'如何綁定到它,因爲你已經得到了輸入。 – 2014-11-08 21:15:29

+0

爲了清楚起見,我在原始答案中添加了評論。 – 2014-11-08 21:18:57