Java写atm取钱程序
时间: 2023-05-12 20:06:22 浏览: 124
以下是一个简单的 Java ATM 取款程序:
```
import java.util.Scanner;
public class ATM {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int balance = 1000; // 初始余额为 1000 元
int withdrawAmount; // 取款金额
System.out.println("欢迎使用 ATM 取款机!");
System.out.println("您的当前余额为 " + balance + " 元。");
// 循环取款直到用户输入 0
while (true) {
System.out.print("请输入取款金额(输入 0 退出):");
withdrawAmount = input.nextInt();
if (withdrawAmount == 0) {
System.out.println("感谢使用 ATM 取款机,再见!");
break;
}
if (withdrawAmount > balance) {
System.out.println("余额不足,请重新输入!");
} else {
balance -= withdrawAmount;
System.out.println("取款成功,您的当前余额为 " + balance + " 元。");
}
}
}
}
```
这个程序会提示用户输入取款金额,然后从余额中扣除相应的金额。如果余额不足,则提示用户重新输入。用户可以输入 0 退出程序。