本帖最后由 黎志勇 于 2014-7-21 21:24 编辑
写了一下。不知道你是不是要写比较器了,我就写了个比较两个IP的。写比较器的可以适当改一下。
- package test;
- public class IpTest {
- public static void main(String[] args) {
- compareIP("61.53.231.249", "61.54.231.245");
- compareIP("61.54.231.246", "61.54.231.48");
- compareIP("61.54.231.48", "61.54.231.9");
- compareIP("192.168.1.1", "192.168.1.1");
- }
- public static void compareIP(String ip1, String ip2) {
- int ip1Bigger = 0;
- String[] ip1Parts = ip1.split("\\.");// 通过"."分隔字符串
- String[] ip2Parts = ip2.split("\\.");
- for (int i = 0; i < ip2Parts.length; i++) {
- ip1Bigger = Integer.parseInt(ip1Parts[i])
- - Integer.parseInt(ip2Parts[i]);
- if (ip1Bigger != 0) {
- break;
- }
- }
- char result = ip1Bigger == 0 ? '=' : (ip1Bigger > 0 ? '>' : '<');
- System.out.println(ip1 + result + ip2);
- }
- }
复制代码
|