题解 | #合法IP#

合法IP

https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner fzhinput = new Scanner(System.in);
        String ipdz = fzhinput.nextLine();
        String ip[] = ipdz.split("\\.");
        int num = 0; 
        if (ip.length == 4) {
            for (int i = 0; i < ip.length; i++) {
                if (ip[i].length()==0 || !Integer.toString(Integer.parseInt(ip[i])).equals(ip[i])) {
                    System.out.println("NO");
                    break;
                } else {
                    int ipsz = Integer.parseInt(ip[i]);
                    if (ipsz <= 255 && ipsz >= 0) {
                        num++;
                    } else {
                        System.out.println("NO");
                        break;
                    }
                }
            }
            if (num == 4) {
                System.out.println("YES");
            }
        } else {
            System.out.println("NO");
        }

    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务