合法IP
题目描述
现在IPV4下用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此不需要用正号出现),如10.137.17.1,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。
现在需要你用程序来判断IP是否合法。
输入描述:
输入一个ip地址
输出描述:
返回判断的结果YES or NO
输入例子:
10.138.15.1
输出例子:
YES
1 import java.util.*; 2 3 public class Main { 4 public static void main(String[]args){ 5 Scanner sc = new Scanner(System.in); 6 while(sc.hasNext()){ 7 String s = sc.nextLine(); 8 String[] str = s.split("\\."); 9 boolean result = true; 10 if(str.length != 4) 11 result = false; 12 13 for(String i : str){ 14 int num = Integer.parseInt(i); 15 //这里的判断一直困扰着我 16 if(num < 0 || num > 255){ 17 result=false; 18 break; 19 } 20 } 21 if(result){ 22 System.out.println("YES"); 23 }else{ 24 System.out.println("NO"); 25 } 26 } 27 } 28 }
if- else -break-continue 的使用问题该总结一下了
浙公网安备 33010602011771号