Java 程序显示系统 IP 地址


2022年4月8日, Learn eTutorial
1418

在这里,我们将解释如何编写一个 Java 程序来显示系统的 IP 地址。

什么是 IP 地址?

IP 地址是 互联网协议 地址。它是与计算机关联的识别号码。

示例:168.212.226.204

如何实现显示系统 IP 地址的 Java 程序?

首先,我们需要声明类 IP。然后创建 InetAddress 类的对象 ip。使用 getHostAddress() 以文本形式显示 IP 地址。getLocalHost() 函数用于获取本地主机地址。

算法

步骤 1:使用 public 修饰符声明类 IP

步骤2:打开main()开始程序,Java程序执行从main()开始

步骤 3:创建 InetAddress 类的对象 ip

步骤 4:然后显示 IP 地址为 ip.getHostAddress()

 

Java 源代码

                                          import java.net.InetAddress;
public class IP 
{
    public static void main(String args[]) throws Exception
    {
        InetAddress ip = InetAddress.getLocalHost();
        System.out.println("IP address of this system is := "+ip.getHostAddress());
    }    
}
                                      

输出

IP address of this system is := 127.0.0.1