3月17日 do while循环语句
案例1:密码验证
方法 1:while 循环
string password = "abc123";
string input; // 未赋值
while(input != password) // 1.先判断(这里会报错)
{
Console.Write("请输入密码:")
input = Console.ReadLine(); // 2.后执行赋值
}
Console.WriteLine("登录成功!");
while 循环的特点:循环条件中不允许出现未赋值变量。
方法 2:do while 循环
string password = "abc123";
string input; // 未赋值
do // do: 执行
{
Console.Write("请输入密码:");
input = Console.ReadLine(); // 先执行赋值
} while(input != password); // 后判断
Console.WriteLine("登录成功!");
do while 循环优点:适合没有初始值的变量,常常发生在需要用户输入的场景。
一、do while语句是什么
do while 是一种先执行一次,后判断的循环语句。
👉 核心特点:
do while 循环特别适合:当条件一开始就不成立
二、基本语法
do
{
// 循环体
} while (条件);
⚠️ 注意:while (条件); 后面必须有分号 ;
三、执行流程
执行顺序如下:
① 先执行一次代码块
② 再判断条件
③ 如果为 true → 继续循环
④ 如果为 false → 结束
四、与 while 的本质区别(重点)
| 对比项 | while | do while |
|---|---|---|
| 判断时机 | 先判断 | 后判断 |
| 执行次数 | 可能 0 次 | 至少 1 次 |
| 使用场景 | 条件驱动 | 必须执行一次 |
示例:
// while
while (false)
{
Console.WriteLine("Hello World");
}
// do while
do
{
Console.WriteLine("Hello World");
} while (false);
👉 输出结果:
- while:一次都不执行
- do while:执行了一次
案例 2:密码验证(进阶版)
string username;
string password;
int count = 0;
do
{
Console.Write("用户名:");
username = Console.ReadLine();
Console.Write("密码:");
password = Console.ReadLine();
if (username == "admin" && password == "123")
{
Console.WriteLine("登录成功!");
break;
}
count++;
Console.WriteLine("登录失败");
} while (count < 3);
if (count == 3)
{
Console.WriteLine("账号已锁定");
}
案例3:游戏菜单
int choice;
do
{
// 菜单一定会先显示 1 次 → 这就是 do while 的优势
Console.WriteLine("===== 游戏菜单 =====");
Console.WriteLine("1. 开始游戏");
Console.WriteLine("2. 退出");
Console.Write("请输入你的选择:");
choice = int.Parse(Console.ReadLine());
// 根据选择做出回应,让程序更完整
if (choice == 1)
{
Console.WriteLine("游戏开始!祝你好运~\n");
}
else if (choice == 2)
{
Console.WriteLine("退出成功,欢迎下次再来!");
}
else
{
Console.WriteLine("输入错误,请重新选择!\n");
}
} while (choice != 2); // 选择不是2就一直循环
✔ 菜单必须先显示一次
幸运抽奖机
做一个抽奖程序:
-
每次按回车抽奖一次
-
随机生成 1~100
-
如果是:
- 1~60 → “谢谢参与”
- 61~90 → “小奖”
- 91~100 → “大奖🎉”
-
输入
q退出
答案
Random rand = new Random();
string input;
do
{
Console.WriteLine("按回车抽奖,输入 q 退出:");
input = Console.ReadLine();
if (input == "q") break;
int num = rand.Next(1, 101);
if (num <= 60)
Console.WriteLine("谢谢参与");
else if (num <= 90)
Console.WriteLine("小奖");
else
Console.WriteLine("大奖🎉");
} while (true);
猜拳小游戏
玩家 vs 电脑:
- 输入:1=石头 2=剪刀 3=布
- 电脑随机出
- 判断输赢
- 可以一直玩,输入 0 退出
答案
Random rand = new Random();
int player;
do
{
Console.WriteLine("1=石头 2=剪刀 3=布(0退出):");
player = int.Parse(Console.ReadLine());
if (player == 0) break;
int computer = rand.Next(1, 4);
Console.WriteLine("电脑出:" + computer);
if (player == computer)
Console.WriteLine("平局");
else if ((player == 1 && computer == 2) ||
(player == 2 && computer == 3) ||
(player == 3 && computer == 1))
Console.WriteLine("你赢了!");
else
Console.WriteLine("你输了");
} while (true);
猜数字
随机一个 1~100 的数:
- 玩家不断猜
- 提示“大了 / 小了”
- 猜中结束
- 统计猜了多少次
答案
Random rand = new Random();
int target = rand.Next(1, 101);
int guess;
int count = 0;
do
{
Console.Write("猜数字:");
guess = int.Parse(Console.ReadLine());
count++;
if (guess > target)
Console.WriteLine("大了");
else if (guess < target)
Console.WriteLine("小了");
} while (guess != target);
Console.WriteLine("猜对了!次数:" + count);
ATM 模拟机
模拟银行操作:
菜单:
1. 查询余额
2. 存钱
3. 取钱
4. 退出
要求:
- 初始余额 1000
- 不断循环操作
- 取钱不能超过余额
答案
double balance = 1000;
int choice;
do
{
Console.WriteLine("1.查询余额");
Console.WriteLine("2.存钱");
Console.WriteLine("3.取钱");
Console.WriteLine("4.退出");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("余额:" + balance);
break;
case 2:
Console.Write("存入金额:");
double save = double.Parse(Console.ReadLine());
balance += save;
break;
case 3:
Console.Write("取出金额:");
double withdraw = double.Parse(Console.ReadLine());
if (withdraw > balance)
Console.WriteLine("余额不足!");
else
balance -= withdraw;
break;
}
} while (choice != 4);
Console.WriteLine("已退出系统");
等级刷怪系统
- 初始等级 1,经验 0
- 每次打怪获得 10~30 经验
- 每 100 经验升级
- 输入
n停止刷怪
答案
Random rand = new Random();
int level = 1;
int exp = 0;
string input;
do
{
Console.WriteLine("是否继续打怪?(y/n)");
input = Console.ReadLine();
if (input == "n") break;
int gain = rand.Next(10, 31);
exp += gain;
Console.WriteLine($"获得经验:{gain},当前经验:{exp}");
if (exp >= 100)
{
level++;
exp -= 100;
Console.WriteLine("升级了!当前等级:" + level);
}
} while (true);
Console.WriteLine("游戏结束");
👉 顺序建议:
数字炸弹 → 猜拳 → 抽奖 → ATM → 刷怪系统
这 5 题其实在训练 5 种能力:
| 题目 | 核心能力 |
|---|---|
| 抽奖 | 随机数 + 条件判断 |
| 猜拳 | 逻辑判断 |
| 数字炸弹 | 循环控制 |
| ATM | 菜单系统(真实业务) |
| 刷怪 | 状态管理(游戏开发) |