Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在循环外部使用readLine()_Java_Readline - Fatal编程技术网

Java 在循环外部使用readLine()

Java 在循环外部使用readLine(),java,readline,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行) 当我一个接一个地做了10次readLine()时,我可以阅读所有的10行。然而,这只取决于最后一行输入后是否有换行符。如果没有明确的换行符,它就不会读取最后一行。有解决办法吗?或者我应该只在for循环中执行readLine()吗 包括代码: BufferedReader r = new BufferedReader(new InputSt

我试图在while循环外使用readLine()。输入为:

4
2 1
1 2 
3 2
2 6 5
3 3
2 4 5
4 2
1 2 4 5
(10行) 当我一个接一个地做了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();
    }
}
BufferedReader r=新的BufferedReader(新的InputStreamReader(System.in));
字符串s;
int lineCount=0;
int testCase=0;
试一试{
如果((s=r.readLine())!=null)
testCase=Integer.parseInt;
testCase=testCase*2;
while(lineCount如果(counter在第一行后面加上4行,再加上9行,你就按2步迭代4次。对我来说就是8


缺少一行。

没有任何代码很难说。添加了代码。如果太长,请告诉我,我可以删掉逻辑。实际上,问题页面和问题中的输入都是9行。您的输入文件中有多余的一行吗?