1. 题目
读题
考查点
2. 解法
思路
代码逻辑
具体实现
自行实现
public class HJ090 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(isValid(sc.nextLine()) ? "YES" : "NO");
}
public static boolean isValid(String ip) {
String[] segments = ip.split("\\.");
if (segments.length != 4) {
return false;
}
for (String segment : segments) {
if (segment.length() == 0) {
return false;
}
for (char ch : segment.toCharArray()) {
if (!Character.isDigit(ch)) {
return false;
}
}
if (segment.length() > 1 && segment.charAt(0) == '0' || Integer.parseInt(segment) > 255 || Integer.parseInt(segment) < 0) {
return false;
}
}
return true;
}
}