Nic*_*Jit -1 java java.util.scanner
我一直在解决一些 hackerrank 基本 JAVA 问题,其中一个问题陈述有以下代码:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
scanner.close();
if(N%2!=0){
System.out.println("Weird");
}
else if(N%2==0 && N<=5 && N>=2){
System.out.println("Not Weird");
}
else if(N%2==0 && N<=20 && N>=6){
System.out.println("Weird");
}
else if(N%2==0 && N>20){
System.out.println("Not Weird");
}}}
Run Code Online (Sandbox Code Playgroud)
你能解释一下“scanner.skip(”(\r\n|[\n\r\u2028\u2029\u0085])吗?“)”我知道它会跳过某些模式,但括号里是什么?
小智 6
这大致意味着跳过换行符
\n、\r、\n\r、\r\n、U+0085 U+2028 U+2029都是字符序列或用作行分隔符的字符。
\n is LF or Line Feed or NewLine
\r is CR or Carriage Return
\r\n CR+LF is used in Windows as newLine sequence
U+0085 NEL is the Unicode character for NExt Line
U+2028 is the Unicode character for Line Separator
U+2029 is the Unicode character for Paragraph Separator
Run Code Online (Sandbox Code Playgroud)
所有这些都用于将文件分成几行。您看到的是正则表达式模式,用于搜索字符串内部。读作:
If you meat \n\r
or |
one of the characters in the group **[\n \r \u2028 \u2029 \u0085]** (those are considered single characters to choose from)
? 0 or 1 times
Run Code Online (Sandbox Code Playgroud)
正则表达式是一个非常强大的工具。如果您需要搜索字符串中的特定模式,我建议您学习它。
| 归档时间: |
|
| 查看次数: |
4787 次 |
| 最近记录: |