Week 1-8: Spring Boot 学习计划完整项目
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
63
week1/day03/GuessNumber.java
Normal file
63
week1/day03/GuessNumber.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package day03;
|
||||
|
||||
/**
|
||||
* 第 3 天:猜数字游戏 —— while 循环 + if 判断
|
||||
* 目标:掌握 while 循环和条件判断的配合使用
|
||||
*
|
||||
* 游戏规则:程序随机生成 1-100 的数字,玩家最多猜 7 次,
|
||||
* 每次猜测后提示"大了"或"小了",猜中或次数用完则结束。
|
||||
*/
|
||||
import java.util.Scanner;
|
||||
import java.util.Random;
|
||||
|
||||
public class GuessNumber {
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
Random random = new Random();
|
||||
|
||||
// 生成 1~100 的随机整数
|
||||
int target = random.nextInt(100) + 1;
|
||||
|
||||
int maxAttempts = 7; // 最多猜 7 次
|
||||
int attempts = 0; // 已猜次数
|
||||
boolean guessed = false;
|
||||
|
||||
System.out.println("========== 猜数字游戏 ==========");
|
||||
System.out.println("我已想好一个 1~100 之间的数字,你有 " + maxAttempts + " 次机会!");
|
||||
|
||||
// while 循环:当还有剩余次数且未猜中时继续
|
||||
while (attempts < maxAttempts && !guessed) {
|
||||
attempts++;
|
||||
System.out.print("\n第 " + attempts + " 次猜测,请输入数字: ");
|
||||
int guess = scanner.nextInt();
|
||||
|
||||
if (guess < 1 || guess > 100) {
|
||||
System.out.println("请输入 1~100 之间的数字!");
|
||||
attempts--; // 不浪费这次机会
|
||||
continue; // 跳过本次循环剩余代码
|
||||
}
|
||||
|
||||
if (guess == target) {
|
||||
System.out.println("\n🎉 恭喜你猜对了!答案就是 " + target + "!");
|
||||
System.out.println("你一共猜了 " + attempts + " 次。");
|
||||
guessed = true;
|
||||
} else if (guess > target) {
|
||||
System.out.println("太大了!");
|
||||
} else {
|
||||
System.out.println("太小了!");
|
||||
}
|
||||
|
||||
// 提示剩余次数
|
||||
if (!guessed && attempts < maxAttempts) {
|
||||
System.out.println("还有 " + (maxAttempts - attempts) + " 次机会。");
|
||||
}
|
||||
}
|
||||
|
||||
// 没猜中的情况
|
||||
if (!guessed) {
|
||||
System.out.println("\n😢 很遗憾,次数用完了!正确答案是 " + target + "。");
|
||||
}
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
}
|
||||
47
week1/day03/MultiplicationTable.java
Normal file
47
week1/day03/MultiplicationTable.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package day03;
|
||||
|
||||
/**
|
||||
* 第 3 天:九九乘法表 —— for 循环嵌套
|
||||
* 目标:理解 for 循环的执行顺序和嵌套逻辑
|
||||
*
|
||||
* 输出效果:
|
||||
* 1×1=1
|
||||
* 1×2=2 2×2=4
|
||||
* 1×3=3 2×3=6 3×3=9
|
||||
* ...
|
||||
* 1×9=9 2×9=18 3×9=27 ... 9×9=81
|
||||
*/
|
||||
public class MultiplicationTable {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("========== 九九乘法表 ==========\n");
|
||||
|
||||
// 外层循环控制行(被乘数 i 从 1 到 9)
|
||||
for (int i = 1; i <= 9; i++) {
|
||||
// 内层循环控制列(乘数 j 从 1 到 i,三角形输出)
|
||||
for (int j = 1; j <= i; j++) {
|
||||
// \t 制表符对齐 \t 制表符对齐
|
||||
System.out.print(j + "×" + i + "=" + (i * j) + "\t");
|
||||
}
|
||||
// 每行结束后换行
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
// 附加练习:完整矩形乘法表(1-9 × 1-9)
|
||||
System.out.println("\n========== 完整矩形乘法表 ==========\n");
|
||||
|
||||
// 先打表头
|
||||
System.out.print(" |");
|
||||
for (int i = 1; i <= 9; i++) {
|
||||
System.out.printf("%4d", i);
|
||||
}
|
||||
System.out.println("\n---+------------------------------------");
|
||||
|
||||
for (int i = 1; i <= 9; i++) {
|
||||
System.out.printf(" %d |", i);
|
||||
for (int j = 1; j <= 9; j++) {
|
||||
System.out.printf("%4d", i * j);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user