帮小慕进行自助取款

来源:13-13 测评作业

富哥营销

2025-05-25

import java.util.InputMismatchException;
import java.util.Scanner;

public class DrawMoney {
    public static void main(String[] args) {
        // 设定最大尝试次数为3次
        int attempts = 3;
        while (attempts > 0) {
            try {
                System.out.println("你还有" + attempts + "次机会");
                System.out.print("请输入取款金额:");
                Scanner sc = new Scanner(System.in);
                int amount = sc.nextInt();

                if (amount % 100 != 0) {
                    System.out.println("取款机只能取100元币值,请重新输入你要取的金额");
                    attempts--;
                    continue;
                }

                if (amount > 1000) {
                    System.out.println("你的余额不足,请重新输入");
                    attempts--;
                    continue;
                }

                System.out.println("取款成功,你取款金额是:" + amount);
                break;

            } catch (InputMismatchException e) {
                System.out.println("取款机只能取整数金额");
                attempts--;
                continue;
            } finally {
                System.out.println("关闭控制台输入");
            }
        }
    }
}


写回答

1回答

彭彭老师

2025-05-26

调用 close() 方法关闭控制台输入

https://img1.sycdn.imooc.com/szimg/51830c680934829b05450140.jpg

0
1
富哥营销
好的,老师。我开始是写上的,但编辑器好像报错,所以,我删了😂😂
2025-05-26
共1条回复

Java零基础入门与实战

Java零基础入门与实战 多场景|多案例|全栈项目

303 学习 · 230 问题

查看课程