本文共 4048 字,大约阅读时间需要 13 分钟。
本文将逐步分析几个典型的Java编程练习题,并提供相应的代码实现和解决方案。
题目描述:
探讨如何使用Java中的String和StringBuffer类来实现字符串的反转。 解决方案:
要实现字符串的反转,可以选择以下两种方法:使用String类:
String类虽然不直接提供反转方法,但可以通过将字符串转换为字符数组并逆序遍历来实现反转。这种方法简单易懂,但不适用于大量数据操作。 使用StringBuffer类:
StringBuffer类提供了reverse()方法,专门用于字符串反转操作。这种方法效率较高,适合处理较长字符串。 示例代码:
public class Main { public static void main(String[] args) { String a = "abcd"; char[] c = a.toCharArray(); System.out.print("使用String反转:"); // 输出反转结果 for (int i = c.length; i > 0; i--) { System.out.print(c[i - 1]); } System.out.println(); StringBuffer sb = new StringBuffer("abcd"); sb.reverse(); System.out.print("使用StringBuffer反转:"); // 输出反转结果 System.out.println(sb.toString()); }} 题目描述:
有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子。假如兔子都不死,问每个月的兔子总数为多少?解决方案:
这是一个经典的斐波那契数列问题。兔子的数量随着月份逐步增加,具体规律如下:代码实现:
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入月份:"); String str = sc.nextLine(); int month = Integer.parseInt(str); int sum = fibonacci(month); System.out.println(sum); } private static int fibonacci(int month) { if (month == 1 || month == 2) { return 1; } else { return fibonacci(month - 1) + fibonacci(month - 2); } }} 题目描述:
编写一个方法来判断一个字符串是否对称。解决方案:
字符串对称的定义是字符串正读和逆读完全一致。可以通过遍历字符串的前半部分,比较每个字符与其对称位置的字符是否相同来实现判断。代码实现:
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入字符串:"); String str = input.next(); method(str); } private static void method(String str) { boolean isSymmetric = true; int length = str.length(); int half = (length - 1) / 2; for (int i = 0; i <= half; i++) { if (str.charAt(i) != str.charAt(length - 1 - i)) { isSymmetric = false; break; } } if (!isSymmetric) { System.out.println("该字符串是不对称的"); } else { System.out.println("该字符串是对称的"); } }} 题目描述:
利用Random类生成四则运算题,随机出10个四则运算。解决方案:
可以通过Random类生成运算符号和随机的数字组合,来创建数学题目。代码实现:
import java.util.Random;public class Main { public static void main(String[] args) { String[] operators = new String[]{"+", "-", "×", "÷"}; int[] numbers = new int[1000]; for (int i = 1; i <= 1000; i++) { numbers[i - 1] = i; } Random random = new Random(); for (int i = 0; i < 10; i++) { int num1 = numbers[random.nextInt(1000)]; int num2 = numbers[random.nextInt(1000)]; String operator = operators[random.nextInt(4)]; System.out.println(num1 + operator + num2 + "="); } }} 题目描述:
编写一个算法来判断一个数n是否为快乐数。解决方案:
快乐数的定义是:将一个正整数替换为它每个位置上的数字的平方和,重复这个过程直到数变为1或进入无限循环。代码实现:
import java.util.Scanner;public class Main { public static void main(String[] args) { System.out.print("请输入一个数:"); Scanner sc = new Scanner(System.in()); int n = sc.nextInt(); System.out.println(isHappy(n)); } private static boolean isHappy(int n) { if (n == 1) { return true; } if (n < 1) { return false; } int count = 0; while (n != 1) { String s = Integer.toString(n); char[] chars = s.toCharArray(); int sum = 0; for (char c : chars) { sum += Character.getNumericValue(c); } n = sum; count++; if (count > 1000) { return false; } } return true; }} 这些代码实现了各类经典的编程练习题,涵盖了字符串操作、数列计算、字符串判断以及随机数生成等多个方面。
转载地址:http://zkhfk.baihongyu.com/