写一个java 示例代码
时间: 2023-05-23 21:07:16 浏览: 42
Sure, here you go:
```
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
This is a simple Java program that will output "Hello, World!" to the console.
相关问题
写一段java示例代码
以下是一个简单的Java示例代码,用于计算两个整数的和:
```
public class AddNumbers {
public static void main(String[] args) {
int num1 = 5;
int num2 = 10;
int sum = num1 + num2;
System.out.println("The sum of " + num1 + " and " + num2 + " is " + sum);
}
}
```
该代码定义了一个AddNumbers类,其中包含一个main方法。该方法定义了两个整数变量num1和num2,并将它们相加,将结果存储在sum变量中。最后,使用System.out.println语句打印出求和的结果。
java写一个udp 客户端示例代码
以下是一个简单的UDP客户端示例代码:
```java
import java.io.*;
import java.net.*;
class UDPClient {
public static void main(String args[]) throws Exception {
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("服务器IP地址");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
```
在这个示例中,我们首先从用户采集输入,然后将输入的字符串转换为字节数组并发送到指定的服务器IP地址和端口号。一旦发送成功,我们可以等待来自服务器的响应,然后将响应数据转换为字符串并打印到控制台。最后,我们关闭了客户端套接字。