在循环外使用readLine()

时间:2014-08-19 16:08:38

标签: java readline

我正在尝试在while循环之外使用readLine()。输入是:

4
2 1
1 2 
3 2
2 6 5
3 3
2 4 5
4 2
1 2 4 5

(10行) 当我执行readLine()10次,一个接一个,我可以读取所有十行。但是,这仅取决于在最后一行输入后是否有换行符。如果没有明确的换行符,它永远不会读取最后一行。有解决方法吗?或者我应该只在for循环中执行readLine()吗?

包括代码:

BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
    String s;
    int lineCount = 0;
    int testCase = 0;
    try {
        if ((s = r.readLine()) != null)
            testCase = Integer.parseInt(s);
        testCase = testCase * 2;
        while (lineCount < testCase) {
            System.out.println("Line Count\t" + lineCount);
            String testCaseString = r.readLine();
            System.out.println(testCaseString);
            String arrayTest = r.readLine();
            System.out.println(arrayTest);
            String bTemp[] = testCaseString.split(" ");
            int k = Integer.parseInt(bTemp[1]);
            String aTemp[] = arrayTest.split(" ");
            int a[] = new int[Integer.parseInt(bTemp[0])];
            int n = 0;
            for (String temp : aTemp) {
                a[n] = Integer.parseInt(temp);
                n++;
            }
            int counter = 0;
            for (int i = 0; i < n; i++) {
                if (counter <= k) {
                    if (a[i] % 2 == 0 && a[i] != 0) {
                        counter++;
                    }
                }
            }
            if (counter == k)
                System.out.println("YES");
            else
                System.out.println("NO");
            lineCount = lineCount + 2;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我已解决的codechef问题:PRGIFT

1 个答案:

答案 0 :(得分:1)

在第一行4后,跟随9行,你迭代4次,步数为2.那对我来说是8。

一个失踪。