gpt4 book ai didi

Java 下一行没有标记,但应该有?

转载 作者:行者123 更新时间:2023-12-01 22:30:04 27 4
gpt4 key购买 nike

public static void flipLines(Scanner input) {
    int i = 0;
    while (input.hasNextLine()) {
        String ignore = input.nextLine();
        if (i >= 0) {
            System.out.println(input.nextLine());
            System.out.println(ignore);
        } i++;
    }
}

我有这个,我正在尝试翻转扫描仪输入的线条。我收到一个 token 错误,表示仅在最后一行读取此文本时没有行。下面是完全翻转后的样子,但您可以在我的版本中看到,由于错误,我缺少最后一行。

did gyre and gimble in the wabe.Twas brillig and the slithy tovesand the mome raths outgrabe.All mimsey were the borogroves,"Beware the Jabberwock, my son,Beware the JubJub bird and shunthe jaws that bite, the claws that catch,the frumious bandersnatch."

这是错误和翻转线程序的输出。我不知道为什么当它应该打印最后一行时它会超出 token 索引。

did gyre and gimble in the wabe.Twas brillig and the slithy tovesand the mome raths outgrabe.All mimsey were the borogroves,"Beware the Jabberwock, my son,Beware the JubJub bird and shunthe jaws that bite, the claws that catch,NoSuchElementException on line 9:No tokens remain in input.  Near input line 9java.util.NoSuchElementException: No tokens remain in input.  Near input line 9    at Scanner._getNoSuchElementException(Scanner.java:1549)    at Scanner.nextLine(Scanner.java:1254)    at flipLines (Line 9)

最佳答案

如果行数是奇数,则不能安全地假设在检查 nextLine 只存在一次后可以调用两次。您需要在 while 循环体内进行额外的检查。

关于Java 下一行没有标记,但应该有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27994230/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com