Java开发实战精华:破解经典习题,提升编程技能

引言
Java作为一种广泛使用的编程语言,具有强大的功能和应用领域。通过解决经典习题,我们可以深入了解Java语言的特性,提升编程技能。本文将围绕Java编程中的经典习题进行解析,帮助读者更好地掌握Java编程。
一、循环与条件判断
1. 水仙花数
题目描述
:通过循环遍历1到1000,检查每个数是否满足各位数字立方和等于该数本身的条件。
代码示例
:
public class NarcissisticNumber {
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) { int sum = 0; int temp = i; while (temp != 0) { int digit = temp % 10; sum += Math.pow(digit, 3); temp /= 10; } if (sum == i) { System.out.println(i + " 是水仙花数"); } } } } 2. 同时被3、5、7整除的数 题目描述 :通过循环找出1到1000范围内满足条件的数。 代码示例 : public class DivisibleNumber { public static void main(String[] args) { for (int i = 1; i <= 1000; i++) { if (i % 3 == 0 && i % 5 == 0 && i % 7 == 0) { System.out.println(i + " 同时被3、5、7整除"); } } } } 3. 累加求和 题目描述 :利用while、do-while、for循环求解100到200的累加和。 代码示例 : public class SumOfNumbers { public static void main(String[] args) { int sum = 0; int i = 100; while (i <= 200) { sum += i; i++; } System.out.println("while循环累加和:" + sum); sum = 0; i = 100; do { sum += i; i++; } while (i <= 200); System.out.println("do-while循环累加和:" + sum); sum = 0; for (i = 100; i <= 200; i++) { sum += i; } System.out.println("for循环累加和:" + sum); } } 二、数学计算与序列 1. 斐波那契数列部分项求和 题目描述 :求1!2!30!的和。 代码示例 : public class FibonacciSum { public static void main(String[] args) { int sum = 0; int factorial = 1; for (int i = 1; i <= 30; i++) { factorial *= i; sum += factorial; } System.out.println("1!2!30!的和:" + sum); } } 2. 数列求和 题目描述 :求13-2333-44973-983993-1003的值。 代码示例 : public class SequenceSum { public static void main(String[] args) { int sum = 13 - 2333 - 44973 - 983993 - 1003; System.out.println("数列求和:" + sum); } } 三、数组操作 1. 奇偶数统计 题目描述 :对整数数组进行遍历,统计奇数和偶数的个数。 代码示例 : public class OddEvenCount { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int oddCount = 0; int evenCount = 0; for (int i = 0; i < array.length; i++) { if (array[i] % 2 == 0) { evenCount++; } else { oddCount++; } } System.out.println("奇数个数:" + oddCount); System.out.println("偶数个数:" + evenCount); } } 2. 数组过滤 题目描述 :从原数组中筛选非零元素,创建新数组。 代码示例 : public class FilterArray { public static void main(String[] args) { int[] array = {0, 1, 2, 3, 4, 5, 0, 7, 8, 9}; int[] newArray = new int[array.length]; int index = 0; for (int i = 0; i < array.length; i++) { if (array[i] != 0) { newArray[index++] = array[i]; } } System.out.println("过滤后的数组:"); for (int i = 0; i < newArray.length; i++) { System.out.print(newArray[i] + " "); } } } 3. 数组元素操作 题目描述 :求和、最大值、最小值,以及数组倒置。 代码示例 : public class ArrayOperation { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; int sum = 0; int max = array[0]; int min = array[0]; int[] newArray = new int[array.length]; for (int i = 0; i < array.length; i++) { sum += array[i]; if (array[i] > max) {
max = array[i];
}
if (array[i] < min) { min = array[i]; } newArray[array.length - 1 - i] = array[i]; } System.out.println("求和:" + sum); System.out.println("最大值:" + max); System.out.println("最小值:" + min); System.out.println("数组倒置:"); for (int i = 0; i < newArray.length; i++) { System.out.print(newArray[i] + " "); } } } 4. 查询数组元素 题目描述 :在给定的数组中查找特定数值。 代码示例 : public class SearchArray { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; int target = 3; boolean found = false; for (int i = 0; i < array.length; i++) { if (array[i] == target) { found = true; break; } } if (found) { System.out.println(target + " 在数组中"); } else { System.out.println(target + " 不在数组中"); } } } 5. 数组排序与调整 题目描述 :将最大值移动到数组首位,以及在已排序数组中插入元素。 代码示例 : public class ArraySortAndInsert { public static void main(String[] args) { int[] array = {5, 3, 8, 2, 1}; int maxIndex = 0; for (int i = 1; i < array.length; i++) { if (array[i] > array[maxIndex]) {
maxIndex = i;
}
}
int temp = array[0];
array[0] = array[maxIndex];
array[maxIndex] = temp;

int[] sortedArray = {1, 2, 3, 4, 5};
int insertValue = 6;
int index = 0;
while (index < sortedArray.length && sortedArray[index] < insertValue) { index++; } System.arraycopy(sortedArray, index, sortedArray, index + 1, sortedArray.length - index); sortedArray[index] = insertValue; System.out.println("最大值移动到数组首位:" + array[0]); System.out.println("在已排序数组中插入元素:" + sortedArray[index]); } } 四、字符串处理 1. 字符计数 题目描述 :统计字符串中特定字符出现次数。 代码示例 : public class CharacterCount { public static void main(String[] args) { String str = "wantyoutoknowonething"; char target = 'o'; int count = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == target) { count++; } } System.out.println(target + " 在字符串中出现的次数:" + count); } } 2. 字符串截取与替换 题目描述 :从字符串中提取日期,替换特定字符。 代码示例 : public class StringOperation { public static void main(String[] args) { String str = "2021-10-01"; String date = str.substring(0, 10); String newStr = str.replace("-", "/"); System.out.println("提取的日期:" + date); System.out.println("替换后的字符串:" + newStr); } }