题解 | #合法IP#
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String str; while (in.hasNextLine()) { // 注意 while 处理多个 case str = in.nextLine(); try{ String[] arr = str.split("\\."); if(arr.length != 4){ System.out.println("NO"); }else{ for(int i = 0; i < str.length(); i++){ if(Integer.parseInt(arr[i]) >= 0 && Integer.parseInt(arr[i]) <= 255 && (arr[i].charAt(0) > '0' && arr[i].charAt(0) <= '9' || Integer.parseInt(arr[i]) == 0)){ if(i == arr.length - 1){ System.out.println("YES"); break; } continue; }else{ System.out.println("NO"); break; } } } }catch(Exception e){ System.out.println("NO"); } } } }