2 y999666 y999666 于 2016.04.08 16:29 提问

java中Scanner中close()的的使用方法? 1C
 package hihocoder;
import java.util.Scanner;

public class CopyOfTrieTree {
    public static void main(String args[]) {
        CopyOfTrieTree m = new CopyOfTrieTree();
        m.getInput(true);
        System.out.println(2222);
        m.getInput(false);
    }

    public void getInput(boolean b) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            String s = in.next();
            if(b)
                System.out.println(s+0);
            else 
                System.out.println(s+1);
        }
        in.close();//为什么这里加上close(),程序就运行错误,不加就是对的
    }

}

如上程序,为什么加上scanner的关闭函数后,程序就运行错误,不加close函数,运行就是对的???

4个回答

qq_34187608
qq_34187608   2016.04.08 17:16

把in换成scan试试

qq_34187608
qq_34187608   2016.04.08 17:17

把错误的结果截一下图

y999666
y999666 1 we Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at hihocoder.TrieTree.getInput(TrieTree.java:18) at hihocoder.TrieTree.main(TrieTree.java:13)
一年多之前 回复
y999666
y999666   2016.04.08 17:50

1
we
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at hihocoder.TrieTree.getInput(TrieTree.java:18)
at hihocoder.TrieTree.main(TrieTree.java:13)

CSDNXIAOD
CSDNXIAOD   2016.04.08 21:45

java基础之Scanner close方法
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!