丑题多作怪!!!
- 不要觉得我代码写的差~
- 因为这是一个奇丑无比的题目~
- 君子绕行~
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] ipSubStr = in.nextLine().split("\\.");
if (ipSubStr.length != 4) {
System.out.println("NO");
return;
}
int[] ipSubInt = new int[ipSubStr.length];
boolean valid = true;
for (int i = 0; i < ipSubStr.length; i++) {
String ipSubs = ipSubStr[i];
if (ipSubs.isEmpty()) {
valid = false;
continue;
}
if (ipSubs.length() > 1 && (ipSubs.charAt(0) < '1' || ipSubs.charAt(0) > '9')) {
valid = false;
continue;
}
try {
ipSubInt[i] = Integer.parseInt(ipSubs);
} catch (Exception e) {
valid = false;
break;
}
if (ipSubInt[i] > 255) {
valid = false;
break;
}
}
System.out.println(valid ? "YES" : "NO");
}
}

京公网安备 11010502036488号