Java doWhile循环案例怎么实操

wen java案例 27

本文目录导读:

Java doWhile循环案例怎么实操

  1. 🎯 案例1:基础语法演示 - 数字打印
  2. 📝 案例2:密码验证系统
  3. 🎲 案例3:猜数字游戏
  4. 💳 案例4:银行ATM取款机
  5. 🎮 案例5:菜单选择循环
  6. 💡 实用技巧总结

我来为你提供几个Java do-while循环的实操案例,从简单到复杂逐步理解。

🎯 案例1:基础语法演示 - 数字打印

public class BasicDoWhile {
    public static void main(String[] args) {
        int i = 1;
        // 先执行一次,再判断条件
        do {
            System.out.println("当前数字: " + i);
            i++;
        } while (i <= 5);
        System.out.println("循环结束,i = " + i);
    }
}

输出结果:

当前数字: 1
当前数字: 2
当前数字: 3
当前数字: 4
当前数字: 5
循环结束,i = 6

📝 案例2:密码验证系统

import java.util.Scanner;
public class PasswordChecker {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String correctPassword = "123456";
        String userInput;
        // 至少让用户输入一次密码
        do {
            System.out.print("请输入密码: ");
            userInput = input.nextLine();
            if (!userInput.equals(correctPassword)) {
                System.out.println("密码错误!请重试!");
            }
        } while (!userInput.equals(correctPassword));
        System.out.println("恭喜!密码正确!登录成功!");
        input.close();
    }
}

🎲 案例3:猜数字游戏

import java.util.Scanner;
import java.util.Random;
public class GuessingGame {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Random random = new Random();
        int target = random.nextInt(100) + 1;  // 生成1-100的随机数
        int guess;
        int attempts = 0;
        System.out.println("=== 猜数字游戏 ===");
        System.out.println("猜一个1-100之间的数字");
        do {
            System.out.print("请输入你的猜测: ");
            guess = input.nextInt();
            attempts++;
            if (guess > target) {
                System.out.println("太大了!再试试");
            } else if (guess < target) {
                System.out.println("太小了!再试试");
            } else {
                System.out.println("🎉 恭喜你猜对了!");
                System.out.println("你一共猜了 " + attempts + " 次");
            }
        } while (guess != target);
        input.close();
    }
}

💳 案例4:银行ATM取款机

import java.util.Scanner;
public class ATMSystem {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double balance = 1000.00;  // 初始余额
        String choice;
        System.out.println("=== 简单ATM系统 ===");
        do {
            System.out.println("\n当前余额: ¥" + balance);
            System.out.println("1. 查询余额");
            System.out.println("2. 存款");
            System.out.println("3. 取款");
            System.out.println("4. 退出");
            System.out.print("请选择操作: ");
            choice = input.nextLine();
            switch (choice) {
                case "1":
                    System.out.println("当前余额: ¥" + balance);
                    break;
                case "2":
                    System.out.print("请输入存款金额: ");
                    double deposit = input.nextDouble();
                    input.nextLine();  // 消耗换行符
                    if (deposit > 0) {
                        balance += deposit;
                        System.out.println("存款成功!新余额: ¥" + balance);
                    } else {
                        System.out.println("存款金额无效!");
                    }
                    break;
                case "3":
                    System.out.print("请输入取款金额: ");
                    double withdraw = input.nextDouble();
                    input.nextLine();  // 消耗换行符
                    if (withdraw > 0 && withdraw <= balance) {
                        balance -= withdraw;
                        System.out.println("取款成功!新余额: ¥" + balance);
                    } else if (withdraw > balance) {
                        System.out.println("余额不足!");
                    } else {
                        System.out.println("取款金额无效!");
                    }
                    break;
                case "4":
                    System.out.println("感谢使用,再见!");
                    break;
                default:
                    System.out.println("无效选择,请重试!");
            }
        } while (!choice.equals("4"));
        input.close();
    }
}

🎮 案例5:菜单选择循环

import java.util.Scanner;
public class MenuSystem {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int choice;
        System.out.println("=== 学生管理系统 ===");
        do {
            System.out.println("\n========== 主菜单 ==========");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 查看所有学生");
            System.out.println("4. 搜索学生");
            System.out.println("0. 退出系统");
            System.out.print("请选择操作: ");
            choice = input.nextInt();
            input.nextLine();  // 消耗换行符
            switch (choice) {
                case 1:
                    System.out.println("执行:添加学生");
                    // 实际开发中这里调用添加学生的方法
                    break;
                case 2:
                    System.out.println("执行:删除学生");
                    break;
                case 3:
                    System.out.println("执行:查看所有学生");
                    break;
                case 4:
                    System.out.println("执行:搜索学生");
                    break;
                case 0:
                    System.out.println("感谢使用,再见!");
                    break;
                default:
                    System.out.println("无效选项,请重新选择!");
            }
        } while (choice != 0);
        input.close();
    }
}

💡 实用技巧总结

1️⃣ do-while与while的区别

// while循环:可能一次都不执行
int i = 10;
while (i < 5) {
    System.out.println("不会执行");
    i++;
}
// do-while循环:至少执行一次
int j = 10;
do {
    System.out.println("至少执行一次");
    j++;
} while (j < 5);

2️⃣ 避免死循环

int count = 0;
do {
    count++;
    if (count > 100) {
        break;  // 设置安全出口
    }
} while (true);  // 要确保有退出条件

3️⃣ 常用场景

  • 用户输入验证:至少需要一次输入
  • 菜单系统:用户不选择退出就一直显示
  • 游戏循环:游戏没结束就一直玩
  • 重试机制:操作失败后重新尝试

这些案例涵盖了do-while循环的主要应用场景,建议你先运行每个案例,理解代码逻辑,然后尝试修改参数或扩展功能,这样能更好地掌握do-while循环的使用。

抱歉,评论功能暂时关闭!