每行有一个IP地址,IP地址的形式为a.b.c.d,其中a、b、c、d都是整数。
可能有多组测试数据,对于每组数据,如果IP地址合法则输出"Yes!”,否则输出"No!”。 合法的IP地址为: a、b、c、d都是0-255的整数。
255.255.255.255 512.12.2.3
Yes! No!
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); next: while (scanner.hasNext()) { String s = scanner.nextLine(); String[] ss = s.split("\\."); if (ss.length != 4) { System.out.println("No!"); break next; } else { for (String s1 : ss) { int i1 = Integer.parseInt(s1); if (!(i1 <= 255 && i1 >= 0)) { System.out.println("No!"); break next; } } System.out.println("Yes"); } } } }
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner scanr=new Scanner(System.in); String str=scanr.nextLine(); if (str.split("\\.").length!=4) System.out.println("No!"); else { for (int i = 0; i < 4; i++) { int value = Integer.parseInt(str.split("\\.")[i]); if (value < 0 || value > 255) { System.out.println("No!"); return; } } System.out.println("Yes!"); } } }
#include<stdio.h>
int main(){
char s[100];
gets(s);
int i=0;
int v=0;
int c=0;
while(s[i]!='\0'){
if(s[i]=='.')
c++;
i++;
}
if(i>15 || c!=3){
printf("No!");
return 0;
}
s[i]='.';
s[i+1]='\0';
i=0;
while(s[i]!='\0'){
while(s[i]!='.'){
v=v*10+(s[i]-'0');
i++;
}
if(v<0 || v>255){
printf("No!");
return 0;
}
v=0;
i++;
}
printf("Yes!");
return 0;
}
public class Main { public String doSome(String string){ String regex="\\."; int record=0; String[] strings=string.split(regex); for(int i=0;i<strings.length;i++){ if(Integer.valueOf(strings[i])>=0&&Integer.valueOf(strings[i])<=255){ record++; } } if(record==4){ return "Yes"; } return "No"; } public static void main(String[] args) { Scanner s=new Scanner(System.in); int size=s.nextInt(); int count=0; String input; Main m=new Main(); String[] strings=new String[size]; while(count<size){ input=s.next(); strings[count]=input; count++; } for(int i=0;i<strings.length;i++){ System.out.println(m.doSome(strings[i])); } } }
package com.lin.niuke; import java.util.Scanner; /** * Created by 9.6 on 2017/5/26. */ public class IpCheck { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ int size = scanner.nextInt(); Scanner sx = new Scanner(System.in); for (int i = 0; i < size; i++) { String ip = sx.nextLine(); if (ip.contains(".")){ if((!ip.endsWith(".")) && (!ip.startsWith("."))){ String[] ipArr = ip.split("\\."); if (ipArr.length == 4){ int j = 0; for ( ;j < ipArr.length; j++) { if (ipArr[j] != "") { if ((Integer.valueOf(ipArr[j]) > 255) || (Integer.valueOf(ipArr[j]) < 0)) { System.out.println("No!"); break; } if (j == (ipArr.length - 1)) { System.out.println("Yes!"); } }else{ System.out.println("No!"); } } }else{ System.out.println("No!"); } }else { System.out.println("No!"); } }else { System.out.println("No!"); } } } } } 各位大神帮忙看看,我自己测试都没有问题,为啥贴上去测试一直报,请检查是否存在数组越界等非法访问情况。