1. 习题一:Java基础
1.1 题目描述
编写一个Java程序,输出“Hello, World!”。
1.2 解答
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. 习题二:数据类型
2.1 题目描述
定义一个整型变量num,赋值为100,然后输出该变量的值。
2.2 解答
public class DataTypeExample {
public static void main(String[] args) {
int num = 100;
System.out.println(num);
}
}
3. 习题三:运算符
3.1 题目描述
编写一个Java程序,计算两个整数的和、差、积、商。
3.2 解答
public class OperatorExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println("和:" + (a + b));
System.out.println("差:" + (a - b));
System.out.println("积:" + (a * b));
System.out.println("商:" + (a / b));
}
}
4. 习题四:控制结构
4.1 题目描述
编写一个Java程序,判断一个整数是否为偶数。
4.2 解答
public class ControlStructureExample {
public static void main(String[] args) {
int num = 10;
if (num % 2 == 0) {
System.out.println(num + "是偶数");
} else {
System.out.println(num + "是奇数");
}
}
}
5. 习题五:循环结构
5.1 题目描述
编写一个Java程序,输出1到100之间的所有偶数。
5.2 解答
public class LoopStructureExample {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
System.out.println(i);
}
}
}
}
6. 习题六:数组
6.1 题目描述
编写一个Java程序,创建一个整型数组,并初始化为1、2、3、4、5,然后输出数组中的所有元素。
6.2 解答
public class ArrayExample {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
7. 习题七:面向对象编程
7.1 题目描述
编写一个Java程序,定义一个名为Person的类,包含姓名和年龄属性,以及一个打印信息的方法。
7.2 解答
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void printInfo() {
System.out.println("姓名:" + name + ",年龄:" + age);
}
public static void main(String[] args) {
Person person = new Person("张三", 20);
person.printInfo();
}
}
8. 习题八:异常处理
8.1 题目描述
编写一个Java程序,尝试除以0,并捕获异常。
8.2 解答
public class ExceptionExample {
public static void main(String[] args) {
try {
int result = 10 / 0;
System.out.println("结果:" + result);
} catch (ArithmeticException e) {
System.out.println("发生异常:" + e.getMessage());
}
}
}
9. 习题九:文件操作
9.1 题目描述
编写一个Java程序,读取一个文本文件,并输出文件中的所有内容。
9.2 解答
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileExample {
public static void main(String[] args) {
String filePath = "example.txt";
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("读取文件失败:" + e.getMessage());
}
}
}
10. 习题十:网络编程
10.1 题目描述
编写一个Java程序,实现一个简单的HTTP服务器。
10.2 解答
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class HttpServer {
public static void main(String[] args) {
int port = 8080;
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("服务器启动,监听端口:" + port);
while (true) {
Socket socket = serverSocket.accept();
new Thread(new HttpHandler(socket)).start();
}
} catch (IOException e) {
System.out.println("服务器启动失败:" + e.getMessage());
}
}
static class HttpHandler implements Runnable {
private Socket socket;
public HttpHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String requestLine = in.readLine();
if (requestLine != null && requestLine.contains("GET")) {
String[] requestParts = requestLine.split(" ");
String filePath = requestParts[1];
File file = new File(filePath);
if (file.exists()) {
byte[] fileContent = readFileToByteArray(file);
out.println("HTTP/1.1 200 OK");
out.println("Content-Type: text/html");
out.println("Content-Length: " + fileContent.length);
out.println();
out.write(fileContent);
} else {
out.println("HTTP/1.1 404 Not Found");
out.println("Content-Type: text/html");
out.println("Content-Length: 0");
out.println();
}
}
} catch (IOException e) {
System.out.println("处理请求失败:" + e.getMessage());
} finally {
try {
socket.close();
} catch (IOException e) {
System.out.println("关闭socket失败:" + e.getMessage());
}
}
}
private byte[] readFileToByteArray(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
}
fis.close();
return bos.toByteArray();
}
}
}
以上是Java语言程序设计第二版郎波课后习题的解答汇总,希望能对您有所帮助。
