import java.io.*;

public class Subnet { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    // 输入要划分的IP地址
    System.out.println("---------------划分子网---------------");
    System.out.println("请输入要划分的IP地址(格式为:x1.x2.x3.x4):");
    System.out.print("→:");
    String ip = br.readLine();

    // 解析IP地址
    String[] ipArray = ip.split("\\.");
    int x1 = Integer.parseInt(ipArray[0]);
    int x2 = Integer.parseInt(ipArray[1]);
    int x3 = Integer.parseInt(ipArray[2]);
    int x4 = Integer.parseInt(ipArray[3]);

    // 根据IP地址类别计算最大子网数和默认子网掩码
    int maxSubnets;
    String defaultSubnetMask;
    if (x1 >= 1 && x1 <= 126) { // A类
        maxSubnets = (int) Math.pow(2, 24 - 2);
        defaultSubnetMask = "255.0.0.0";
    } else if (x1 >= 128 && x1 <= 191) { // B类
        maxSubnets = (int) Math.pow(2, 16 - 2);
        defaultSubnetMask = "255.255.0.0";
    } else if (x1 >= 192 && x1 <= 223) { // C类
        maxSubnets = (int) Math.pow(2, 8 - 2);
        defaultSubnetMask = "255.255.255.0";
    } else {
        System.out.println("无效的IP地址");
        return;
    }
    System.out.println("本IP地址能划分划分的最大子网数为: " + maxSubnets);

    // 输入要划分的子网数
    System.out.println("请输入要划分的子网数:");
    System.out.print("→:");
    int s = Integer.parseInt(br.readLine());

    // 计算子网掩码
    int n = (int) Math.ceil(Math.log(s) / Math.log(2));
    int subnetMask = 0xffffffff << (32 - n);
    String subnetMaskStr = String.format("%d.%d.%d.%d", (subnetMask >> 24) & 0xff, (subnetMask >> 16) & 0xff,
            (subnetMask >> 8) & 0xff, subnetMask & 0xff);
    System.out.println("子网掩码为:" + subnetMaskStr);

    // 计算每个子网的主机数
    double hostPerSubnet = Math.pow(2, 32 - n) - 2;
    System.out.println("每个子网的主机数:" + (int) hostPerSubnet);

    // 计算每个子网的网络地址和主机地址范围
    int startingAddress = (x1 << 24) | (x2 << 16) | (x3 << 8) | x4;
    for (int i = 0; i < s; i++) {
        int networkAddress = startingAddress + i * (int) Math.pow(2, 32 - n);
        int broadcastAddress = networkAddress + (int) Math.pow(2, 32 - n) - 1;
        int firstHostAddress = networkAddress + 1;
        int lastHostAddress = broadcastAddress - 1;
        String networkAddressStr = String.format("%d.%d.%d.%d", (networkAddress >> 24) & 0xff,
                (networkAddress >> 16) & 0xff, (networkAddress >> 8) & 0xff, networkAddress & 0xff);
        String broadcastAddressStr = String.format("%d.%d.%d.%d", (broadcastAddress >> 24) & 0xff,
                (broadcastAddress >> 16) & 0xff, (broadcastAddress >> 8) & 0xff, broadcastAddress & 0xff);
        String firstHostAddressStr = String.format("%d.%d.%d.%d", (firstHostAddress >> 24) & 0xff,
                (firstHostAddress >> 16) & 0xff, (firstHostAddress >> 8) & 0xff, firstHostAddress & 0xff);
        String lastHostAddressStr = String.format("%d.%d.%d.%d", (lastHostAddress >> 24) & 0xff,
                (lastHostAddress >> 16) & 0xff, (lastHostAddress >> 8) & 0xff, lastHostAddress & 0xff);
        System.out.print("<" + (i + 1) + ">");
        System.out.println(" 网络地址:" + networkAddressStr + " 主机地址范围:" + firstHostAddressStr + "-"
                + lastHostAddressStr + " 广播地址:" + broadcastAddressStr);
    }
    System.out.println("子网划分完毕");

    br.close();
}
修改以下代码输入要划分的子网地址、子网数输出划分后的子网掩码、网络地址、主机数、各子网ip范围:import javaio; public class ffdg 		 param args	 	public static void mainString argsthrows IOException 		 TODO Auto-generated method stub		String ip; 用于

原文地址: https://www.cveoy.top/t/topic/fDnj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录