题解 | #合法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 input = new Scanner(System.in);
        String s = input.nextLine();
        String[] split = s.split("\\.");
        if (split.length != 4) {
            System.out.println("NO");
            return;
        }
        try {
            for (String s1 : split) {//超出范围
                if (!(Integer.valueOf(s1) >= 0 && Integer.valueOf(s1) <= 255)) {
                    System.out.println("NO");
                    return;
                }
                if (s1.startsWith("0") && s1.length() > 1) {//0开头的多位数字
                    System.out.println("NO");
                    return;
                }
                if (s1.contains("+") || s1.contains("-")) {//包含正负号
                    System.out.println("NO");
                    return;
                }
            }
            System.out.println("YES");
        } catch (Exception e) {//异常控制其他情况
            System.out.println("NO");
        }



    }
}

全部评论

相关推荐

04-02 16:49
门头沟学院 Java
_bloodstream_:我也面了科大讯飞,主管面的时候听说急招人优先考虑能尽快实习的,我说忙毕设,后面就一直没消息了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务