我应该写什么来代替 "scanner.skip()" 语句?

What do I write in place of "scanner.skip()" statement?

    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));

    int q = scanner.nextInt();
   scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

这个skip()方法有什么用?

我可以用另一种方法替换它以获得相同的结果吗?

让我们阅读 skip 的文档:

Skips input that matches a pattern constructed from the specified string.

所以skip告诉扫描仪不要读取用户输入的某些部分,并在这些部分之后继续。在这里,您的模式匹配 Windows 换行符 \r\n,或其中之一...

  • U+2028 行分隔符
  • U+2029 段落分隔符
  • U+0085 下一行 (NEL)

...如果存在这样的模式。

程序员为什么要这样写?一个可能的解释是避免下一次调用 nextLine 返回空字符串。请参阅 this question 了解发生这种情况的原因。

Would i replace with another method to get the same result?

您可以调用 skip(Pattern.compile("...")),但实际上它只是同一方法的另一个重载。与您所做的最接近的可能是 nextLine,它使用与您类似的模式。