2017-09-13 78 views
-1

e.g操作)接近掃描器(System.in)後,創建掃描儀(System.in),但不能正確

public void a(){ 
    Scanner input = new Scanner(System.in); 
    String a = input.nextLine(); 
    .... 
    input.close(); 
} 

public void b(){ 
    Scanner scan = new Scanner(System.in); 
    int b = scan.nextInt(); 
    .... 
    scan.close(); 
} 

此掃描儀不能正常工作。 但刪除close()後,它可以正常工作。

我猜原因是'System.in'有問題。

請給我答案。

謝謝

+)首先調用input.nextLine()的(),然後調用scan.nextInt(b)中()。 我有錯誤,例如NoSuchElementException。

+0

*試圖掃描儀已經關閉將導致IllegalStateException後執行搜索操作進行更換。*是你得到了什麼? – nullpointer

回答

0
public void b(){ 
    Scanner scan = new Scanner(System.in); 
    int b = scan.nextLine(); 
    .... 
    input.close(); 
} 

這不應該是input.close();b()方法不能訪問方法的變量a()方法。所以,你應該用scan.close()

public void b(){ 
    Scanner scan = new Scanner(System.in); 
    int b = scan.nextLine(); 
    .... 
    scan.close(); 
}