Appearance
7.6 循环案例练习
练习目标
通过以下练习,巩固对 Java 循环语句(for、while、do…while)以及 break 和 continue 关键字的理解和使用。
练习 1:打印图形
题目描述
使用循环打印以下图形:
*
**
***
****
*****参考代码
java
public class PrintTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}练习 2:打印九九乘法表
题目描述
使用循环打印九九乘法表。
参考代码
java
public class MultiplicationTable {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " × " + i + " = " + (i * j) + "\t");
}
System.out.println();
}
}
}练习 3:计算 1 到 100 的和
题目描述
使用循环计算 1 到 100 的和。
参考代码
java
public class Sum1To100 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println("Sum of 1 to 100: " + sum);
}
}练习 4:计算阶乘
题目描述
使用循环计算一个数的阶乘。例如,5 的阶乘是 5 × 4 × 3 × 2 × 1 = 120。
参考代码
java
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = scanner.nextInt();
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
System.out.println(n + "! = " + factorial);
scanner.close();
}
}练习 5:猜数字游戏
题目描述
使用循环实现一个猜数字游戏:
- 系统随机生成一个 1 到 100 之间的数字
- 用户输入猜测的数字
- 系统提示"Too high!"或"Too low!"
- 直到用户猜对为止
参考代码
java
import java.util.Scanner;
import java.util.Random;
public class GuessNumberGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int secretNumber = random.nextInt(100) + 1;
int guess = 0;
int attempts = 0;
System.out.println("Guess a number between 1 and 100:");
while (guess != secretNumber) {
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
attempts++;
if (guess < secretNumber) {
System.out.println("Too low!");
} else if (guess > secretNumber) {
System.out.println("Too high!");
} else {
System.out.println("Congratulations! You guessed the number in " + attempts + " attempts.");
}
}
scanner.close();
}
}练习 6:打印斐波那契数列
题目描述
使用循环打印斐波那契数列的前 10 项。斐波那契数列的定义是:前两项为 0 和 1,后续每一项都是前两项的和。
参考代码
java
public class Fibonacci {
public static void main(String[] args) {
int n = 10;
int a = 0, b = 1;
System.out.print(a + " " + b + " ");
for (int i = 3; i <= n; i++) {
int c = a + b;
System.out.print(c + " ");
a = b;
b = c;
}
System.out.println();
}
}练习 7:查找数组中的最大值
题目描述
使用循环查找数组中的最大值。
参考代码
java
public class FindMax {
public static void main(String[] args) {
int[] numbers = {5, 3, 9, 1, 7, 2, 8, 4, 6};
int max = numbers[0];
for (int number : numbers) {
if (number > max) {
max = number;
}
}
System.out.println("Maximum value: " + max);
}
}练习 8:计算平均值
题目描述
使用循环计算数组中所有元素的平均值。
参考代码
java
public class CalculateAverage {
public static void main(String[] args) {
int[] numbers = {5, 3, 9, 1, 7, 2, 8, 4, 6};
int sum = 0;
for (int number : numbers) {
sum += number;
}
double average = (double) sum / numbers.length;
System.out.println("Average: " + average);
}
}练习 9:打印素数
题目描述
使用循环打印 1 到 100 之间的所有素数。素数是指只能被 1 和自身整除的正整数。
参考代码
java
public class PrintPrimes {
public static void main(String[] args) {
System.out.println("Prime numbers between 1 and 100:");
for (int i = 2; i <= 100; i++) {
boolean isPrime = true;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(i + " ");
}
}
System.out.println();
}
}练习 10:反转字符串
题目描述
使用循环反转一个字符串。例如,将 "Hello" 反转为 "olleH"。
参考代码
java
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
String reversed = "";
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input.charAt(i);
}
System.out.println("Reversed string: " + reversed);
scanner.close();
}
}练习 11:统计字符出现次数
题目描述
使用循环统计字符串中某个字符出现的次数。
参考代码
java
import java.util.Scanner;
public class CountCharacter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
System.out.print("Enter the character to count: ");
char target = scanner.next().charAt(0);
int count = 0;
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == target) {
count++;
}
}
System.out.println("The character '" + target + "' appears " + count + " times.");
scanner.close();
}
}练习 12:打印菱形
题目描述
使用循环打印一个菱形。
参考代码
java
public class PrintDiamond {
public static void main(String[] args) {
int n = 5;
// 打印上半部分
for (int i = 1; i <= n; i++) {
for (int j = n; j > i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
// 打印下半部分
for (int i = n - 1; i >= 1; i--) {
for (int j = n; j > i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}总结
通过这些练习,你应该已经掌握了 Java 循环语句的使用方法,包括:
- for 循环:适用于已知循环次数的场景
- while 循环:适用于循环次数不确定的场景
- do…while 循环:适用于至少需要执行一次的场景
- break 和 continue 关键字:用于控制循环流程
- 嵌套循环:用于处理复杂的问题
这些循环语句是 Java 编程中的基础,掌握它们对于编写复杂的程序非常重要。
