【发布时间】:2014-12-02 10:31:35
【问题描述】:
假设我有以下示例代码:
Scanner scan1 = new Scanner(System.in); // declaring new Scanner called scan1
int x = scan1.nextInt(); // scan for user input and set it to x
System.out.println(x); // print the value of x
scan1.close(); // closes the scanner (I don't know exactly what this does)
Scanner scan2 = new Scanner(System.in); // declaring new Scanner called scan1
int y = scan2.nextInt(); // scan for user input and set it to y
System.out.println(y); // print the value of y
我阅读了Oracle documentation on the Scanner class 并遇到了这个:
当 Scanner 关闭时,如果源实现了 Closeable 接口,它将关闭其输入源。
这是否意味着一旦Scanner(System.in)关闭,我将无法再在整个 Java 程序中使用System.in?或者这是否意味着我将不再能够在整个课堂上使用它?还是只有方法?还是只有它的范围?
我的另一个问题是,Scanner 是否仅限于声明它的范围(类似于原始数据类型)?
【问题讨论】: