这个ip可真是键啊,先是+0. 然后+. 然后.1.3.8 然后01.3.8.4 服了 可真是气死我了
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String str = in.next();
String[] res = str.split("\\.");
int count = 0;
for (int i = 0; i < res.length; i++) {
if (res[i].equals("")) {//空字段不合法
break;
}
for (int j = 0; j < res[i].length(); j++) {
if (!Character.isDigit(res[i].charAt(j)))//+号等非数字字符不合法
count--;
}
int n = Integer.parseInt(res[i]); //空子段不可以转化,很多都错在了这里哪.
if((res[i].startsWith("0") && n != 0))break;//防止这种类型,多个0,01.3.8.4
if (n >= 0 && n <= 255) {
count += 1;
}
}
if (count == 4) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}