现在的位置: 首页 > 综合 > 正文

IP加步长

2018年05月09日 ⁄ 综合 ⁄ 共 945字 ⁄ 字号 评论关闭
package com.huahua.test;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		@SuppressWarnings("resource")
		Scanner cin = new Scanner(System.in);
		
		String startStr = "";
		String stepStr = "";
		
		while (cin.hasNext()){
			startStr = cin.nextLine();
			stepStr = cin.nextLine();
			
			String startArr[] = startStr.split("\\.");
			String stepArr[] = stepStr.split("\\.");
			
			if(startArr.length!=4||stepArr.length!=4){
				System.out.println("err");
				return;
			}
			
			Integer startInt[] = new Integer[4];
			Integer stepInt[] = new Integer[4];
			
			for (int i = 0; i < 4; i++) {
				startInt[i] = Integer.parseInt(startArr[i]);
				stepInt[i] = Integer.parseInt(stepArr[i]);
				
				if(startInt[i]<0||startInt[i]>255||stepInt[i]<0||stepInt[i]>255){
					System.out.println("err");
					return;
				}
			}

			String endStr="";
			int num=0;
			for (int i = 0; i < 4; i++) {
				num = startInt[i]+stepInt[i];
				if(num>255){
					continue;
				}
				
				if(i<3){
					endStr+= Integer.toString(num)+".";
				}else{
					endStr+= Integer.toString(num);
				}
			}
			
			System.out.println(endStr);
			return;
		}
		
	}

}

用例:

1.1.1.1
2.2.2.2
3.3.3.3

【上篇】
【下篇】

抱歉!评论已关闭.