帮小慕进行自助取款
来源: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() 方法关闭控制台输入
012025-05-26
相似问题