编程心得: 编程是一个堆积木的过程,首先要实现功能,不论方法多么笨,功能实现之后再去考虑代码的优化问题;
String ip1 = "192.168.1.16";
String ip2 = "192.168.1.18";
String[] ipArr1 = ip1.split("//."); //点号是正则表达式中预定义字符类,需要转义
String[] ipArr2 = ip2.split("//.");
int[] ipArr3 = new int[4];
int[] ipArr4 = new int[4];
//版本三
for(int i=0;i<4;i++){
ipArr3[i] = Integer.parseInt(ipArr1[i]);
ipArr4[i] = Integer.parseInt(ipArr2[i]);
//在为整型数组赋值的时候,我们就对两个ip的各阶段值进行比较大小
if(ipArr3[i] > ipArr4[i]){
System.out.println("ip1 > ip2");
}else if(ipArr3[i] < ipArr4[i]){
System.out.println("ip1 < ip2");
}else{
if(i==3) System.out.println("ip1==ip2");
continue;
}
}
/* 版本二
for(int i=0;i<4;i++){
if(ipArr3[i] > ipArr4[i]){
System.out.println("ip1 > ip2");
}else if(ipArr3[i] < ipArr4[i]){
System.out.println("ip1 < ip2");
}else{
if(i==3) System.out.println("ip1==ip2");
continue;
}
}
*/
/* 版本一
if(ipArr3[0] > ipArr4[0]){
System.out.println("ip1 > ip2");
}else if(ipArr3[0] < ipArr4[0]){
System.out.println("ip1 < ip2");
}else{
if(ipArr3[1] > ipArr4[1]){
System.out.println("ip1 > ip2");
}else if(ipArr3[1] < ipArr4[1]){
System.out.println("ip1 < ip2");
}else{
if(ipArr3[2] > ipArr4[2]){
System.out.println("ip1 > ip2");
}else if(ipArr3[2] < ipArr4[2]){
System.out.println("ip1 < ip2");
}else{
if(ipArr3[3] > ipArr4[3]){
System.out.println("ip1 > ip2");
}else if(ipArr3[3] < ipArr4[3]){
System.out.println("ip1 < ip2");
}else{
System.out.println("ip1 = ip2");
}
}
}
}
*/