三人丿木丶一兀之


私信TA

用户名:792408237

访问量:10895

签 名:

等  级
排  名 233
经  验 6019
参赛次数 14
文章发表 36
年  龄 0
在职情况 学生
学  校 木星土里挖中心技术学院
专  业

  自我简介:

解题思路:

注意事项:

参考代码:

//导入命名空间

import java.util.Scanner;

public class IP判断 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner scanner = new Scanner(System.in);
  //hasnext:没当你输入多少数据它才会进行判断
  while (scanner.hasNext()) {
   String strings = scanner.nextLine();

    //根据“.”隔离数据并返回数组形式的数据
   String[] resultStrings = strings.split("\\.");
   boolean l = true;
   for (int i = 0; i < resultStrings.length; i++) {

    //判断它是不是数字 为false直接跳出循环
     l = resultStrings[i].matches("[0-9]+");
     if (l) {

      //如果是数字则还需判断它是否再【0-255】之间 不在则之间跳出循环
     if (Integer.parseInt(resultStrings[i])<0||Integer.parseInt(resultStrings[i])>255) {
      l=false;
      break;
     }
    }else {
     break;
    }
   }
   
   //定义的全局变量,根据上面的判断来得到结果
   if (l) {
    System.out.println("Y");
   } else {
    System.out.println("N");
   }

  }
 }

}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

大坑啊!!!要把+改成*号。否则一直输出N
  //判断它是不是数字 为false直接跳出循环
     l = resultStrings[i].matches("[0-9]+");

正则表达式应该这样写
[0-9]*是一个正则表达式,表示:任意多个数字
 resultStrings[i].matches("[0-9]*");

判断resultStrings是否为数字组成的字符串
2021-11-23 17:03:45
  • «
  • 1
  • »