实战:基础综合
本课是Phase 1的综合实战,通过4个项目巩固前面学到的知识。
项目1:九九乘法表
综合运用:for循环、嵌套循环、格式化输出。
需求
打印完整的九九乘法表。
实现
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.printf("%d×%d=%-4d", j, i, i * j);
}
System.out.println();
}
}
}
输出
TEXT
1×1=1
1×2=2 2×2=4
1×3=3 2×3=6 3×3=9
1×4=4 2×4=8 3×4=12 4×4=16
1×5=5 2×5=10 3×5=15 4×5=20 5×5=25
1×6=6 2×6=12 3×6=18 4×6=24 5×6=30 6×6=36
1×7=7 2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49
1×8=8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64
1×9=9 2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81
知识点
- 嵌套for循环
System.out.printf()格式化输出%-4d表示左对齐,占4个字符
项目2:冒泡排序
综合运用:数组、嵌套循环、变量交换。
需求
对数组进行升序排序。
实现
JAVA
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
System.out.println("排序前:" + Arrays.toString(arr));
// 冒泡排序
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
// 交换
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("排序后:" + Arrays.toString(arr));
}
}
输出
TEXT
排序前:[64, 34, 25, 12, 22, 11, 90]
排序后:[11, 12, 22, 25, 34, 64, 90]
知识点
- 数组遍历与元素交换
- 嵌套循环的控制
Arrays.toString()打印数组
项目3:猜数字游戏
综合运用:Random随机数、Scanner输入、while循环、条件判断。
需求
程序随机生成1-100的数字,用户猜测,程序提示太大/太小/正确。
实现
JAVA
import java.util.Random;
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int target = random.nextInt(100) + 1;
int guess = 0;
int attempts = 0;
System.out.println("=== 猜数字游戏 ===");
System.out.println("我想了一个1-100的数字,你来猜!");
while (guess != target) {
System.out.print("请输入你的猜测:");
guess = scanner.nextInt();
attempts++;
if (guess > target) {
System.out.println("太大了!再试试。");
} else if (guess < target) {
System.out.println("太小了!再试试。");
} else {
System.out.println("恭喜你猜对了!答案就是 " + target);
System.out.println("你一共猜了 " + attempts + " 次");
}
}
scanner.close();
}
}
输出示例
TEXT
=== 猜数字游戏 ===
我想了一个1-100的数字,你来猜!
请输入你的猜测:50
太大了!再试试。
请输入你的猜测:25
太小了!再试试。
请输入你的猜测:37
太大了!再试试。
请输入你的猜测:31
恭喜你猜对了!答案就是 31
你一共猜了 4 次
知识点
Random.nextInt()生成随机数Scanner.nextInt()获取用户输入- while循环实现重复猜测
- if-else if-else 多条件判断
项目4:简易计算器
综合运用:Scanner输入、switch语句、算术运算、异常处理。
需求
用户输入两个数和运算符,程序计算并输出结果。
实现
JAVA
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("=== 简易计算器 ===");
System.out.print("请输入第一个数:");
double num1 = scanner.nextDouble();
System.out.print("请输入运算符(+ - * /):");
char operator = scanner.next().charAt(0);
System.out.print("请输入第二个数:");
double num2 = scanner.nextDouble();
double result = 0;
boolean valid = true;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("错误:除数不能为0");
valid = false;
}
break;
default:
System.out.println("错误:不支持的运算符 '" + operator + "'");
valid = false;
}
if (valid) {
System.out.printf("%.2f %c %.2f = %.2f%n", num1, operator, num2, result);
}
scanner.close();
}
}
输出示例
TEXT
=== 简易计算器 ===
请输入第一个数:10
请输入运算符(+ - * /):*
请输入第二个数:5
10.00 * 5.00 = 50.00
知识点
scanner.next().charAt(0)读取单个字符- switch语句处理多种运算
System.out.printf()格式化输出- 除数为0的判断
综合练习
练习1:数组统计
输入10个成绩,计算平均分、最高分、最低分。
JAVA
import java.util.Scanner;
import java.util.Arrays;
public class ScoreStats {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] scores = new int[10];
// 输入成绩
for (int i = 0; i < scores.length; i++) {
System.out.print("请输入第" + (i + 1) + "个成绩:");
scores[i] = scanner.nextInt();
}
// 计算统计
int sum = 0, max = scores[0], min = scores[0];
for (int score : scores) {
sum += score;
if (score > max) max = score;
if (score < min) min = score;
}
double avg = (double) sum / scores.length;
// 输出结果
System.out.println("\n=== 统计结果 ===");
System.out.println("成绩:" + Arrays.toString(scores));
System.out.printf("平均分:%.1f%n", avg);
System.out.println("最高分:" + max);
System.out.println("最低分:" + min);
scanner.close();
}
}
❓ 常见问题
Q 项目写不出来怎么办?
A 先看需求分析,理清思路,再参考代码。不要直接复制,要理解每一行的作用。
Q 代码能运行但结果不对?
A 用System.out.println()打印中间变量,检查每一步的计算结果。
Q 如何提高编程能力?
A 多写多练,从简单项目开始,逐步增加难度。
📖 小节
- 九九乘法表:嵌套循环 + 格式化输出
- 冒泡排序:数组 + 嵌套循环 + 元素交换
- 猜数字游戏:Random + Scanner + while循环
- 简易计算器:Scanner + switch + 算术运算
📝 作业
- 改进计算器: 支持取余运算,支持连续计算
- 数组操作: 输入10个数,找出其中的素数
- 猜数字改进: 限制猜测次数(最多7次),显示历史猜测记录
下一课
下一课我们将进入Phase 2,学习 方法基础,了解如何定义和使用方法。



