第二篇完整链接:Java编程干货练习题(2)
基础篇
1. 输出Hello World 编写一个Java程序,输出字符串“Hello, World!”到控制台。
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. 计算圆的面积 编写一个Java程序,计算给定半径的圆的面积。
import java.util.Scanner;
public class CircleArea {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
double area = Math.PI * radius * radius;
System.out.println("The area of the circle is: " + area);
}
}
3. 判断一个数是否为素数 编写一个Java程序,判断一个整数是否为素数。
public class PrimeNumber {
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
public static void main(String[] args) {
int number = 29;
if (isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
}
中级篇
4. 数组排序 编写一个Java程序,对一个整型数组进行升序排序。
1public class ArraySort {
2 public static void sortArray(int[] arr) {
3 for (int i = 0; i < arr.length - 1; i++) {
4 for (int j = 0; j < arr.length - 1 - i; j++) {
5 if (arr[j] > arr[j + 1]) {
6 // Swap elements
7 int temp = arr[j];
8 arr[j] = arr[j + 1];
9 arr[j + 1] = temp;
10 }
11 }
12 }
13 }
14
15 public static void main(String[] args) {
16 int[] array = {5, 2, 8, 1, 9};
17 sortArray(array);
18 for (int num : array) {
19 System.out.print(num + " ");
20 }
21 }
22}
5. 字符串反转 编写一个Java程序,反转一个字符串。
1public class ReverseString {
2 public static String reverse(String str) {
3 StringBuilder sb = new StringBuilder(str);
4 return sb.reverse().toString();
5 }
6
7 public static void main(String[] args) {
8 String original = "Hello";
9 String reversed = reverse(original);
10 System.out.println("Original: " + original);
11 System.out.println("Reversed: " + reversed);
12 }
13}
高级篇
6. 文件读写 编写一个Java程序,从文件中读取数据并写入另一个文件。
1import java.io.BufferedReader;
2import java.io.BufferedWriter;
3import java.io.FileReader;
4import java.io.FileWriter;
5import java.io.IOException;
6
7public class FileCopy {
8 public static void copyFile(String sourcePath, String destinationPath) throws IOException {
9 BufferedReader reader = null;
10 BufferedWriter writer = null;
11 try {
12 reader = new BufferedReader(new FileReader(sourcePath));
13 writer = new BufferedWriter(new FileWriter(destinationPath));
14
15 String line;
16 while ((line = reader.readLine()) != null) {
17 writer.write(line);
18 writer.newLine();
19 }
20 } finally {
21 if (reader != null) reader.close();
22 if (writer != null) writer.close();
23 }
24 }
25
26 public static void main(String[] args) {
27 String source = "source.txt";
28 String destination = "destination.txt";
29 try {
30 copyFile(source, destination);
31 System.out.println("File copied successfully.");
32 } catch (IOException e) {
33 System.err.println("Error copying file: " + e.getMessage());
34 }
35 }
36}
7. 简单的计算器 编写一个简单的Java计算器程序,支持加减乘除四种运算。
1import java.util.Scanner;
2
3public class SimpleCalculator {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6
7 System.out.print("Enter first number: ");
8 double num1 = scanner.nextDouble();
9 System.out.print("Enter second number: ");
10 double num2 = scanner.nextDouble();
11 System.out.print("Enter an operator (+, -, *, /): ");
12 char operator = scanner.next().charAt(0);
13
14 double result;
15 switch (operator) {
16 case '+':
17 result = num1 + num2;
18 break;
19 case '-':
20 result = num1 - num2;
21 break;
22 case '*':
23 result = num1 * num2;
24 break;
25 case '/':
26 if (num2 != 0) {
27 result = num1 / num2;
28 } else {
29 System.out.println("Cannot divide by zero.");
30 return;
31 }
32 break;
33 default:
34 System.out.println("Invalid operator.");
35 return;
36 }
37 System.out.println("Result: " + result);
38 }
39}