参考代码:

import java.util.*;


public class Main {

    

    public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);

    while (sc.hasNext())

    {

        while(true)

        {

            String str=sc.nextLine();

            char ch;

            if(!((ch=str.charAt(0))<='9'&&ch>'0'))

            {

                System.out.println('N');

                break;

            }

            int index1=str.indexOf('.');

            String str1=str.substring(0,index1);


            char ch1;

            if(!((ch1=str1.charAt(0))<='9'&&ch1>'0'))

            {

                System.out.println('N');

                break;

            }

//处理第一个数字

            int a1=Integer.parseInt(str1);

            if(!(a1>=0&&a1<=255))

            {

                System.out.println('N');

                break;

            }

//处理第二个数字

            int index2=str.indexOf('.',index1+1);

            String str2=str.substring(index1+1,index2);


            char ch2;

            if(!((ch2=str2.charAt(0))<='9'&&ch2>'0'))

            {

                System.out.println('N');

                break;

            }

            int a2=Integer.parseInt(str2);

            if(!(a2>=0&&a2<=255))

            {

                System.out.println('N');

                 break;

            }

//处理第三个数字

            int index3=str.indexOf('.',index2+1);

            String str3=str.substring(index2+1,index3);


            char ch3;

            if(!((ch3=str3.charAt(0))<='9'&&ch3>'0'))

            {

                System.out.println('N');

                    break;

            }

            int a3=Integer.parseInt(str3);

            if(!(a3>=0&&a3<=255))

            {

                System.out.println('N');

                break;

            }

//处理第四个数字

            String str4=str.substring(index3+1);


            char ch4;

            if(!((ch4=str4.charAt(0))<='9'&&ch4>'0'))

            {

                System.out.println('N');

                break;

            }

            int a4=Integer.parseInt(str4);

            if(!(a4>=0&&a4<=255))

            {

                System.out.println('N');

                break;

            }

            System.out.println('Y');

            }

        }

    }

}


点赞(1)
 

0.0分

4 人评分

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 0 条评论

暂无评论