JAVA基础-----模拟一个简单的ATM取款存款提款机.

classATMpublicintID;publicStringname;publicdoublebalance;//余额publicintpassword;//密码publicvoidDrawMoney(doublemoney)//取钱方法balance-=money;System.out.println("当前余额为:" balance);}publicvoidSaveMoney(double...

class ATM {public int ID;public String name;public double balance;// 余额public int password;// 密码public void DrawMoney(double money) {// 取钱方法balance -= money;System.out.println("当前余额为:"balance);}public void SaveMoney(double money) {// 存款方法balance  = money;System.out.println("当前余额为:"balance);}}public class Demo7_ATM {public static void main(String[] args) {Scanner sc = new Scanner(System.in);ATM atm = new ATM();atm.balance = 1000;atm.password = 123;atm.ID = 4569;atm.name = "王思聪";int i = 3;// 定义密码次数System.out.println("欢迎使用ATM自动银行");while (true) {// 循环输入密码System.out.println("请您的输入密码");int pass = sc.nextInt();if (pass == atm.password) {System.out.println("密码正确");System.out.println("你的ID:"atm.ID"名字:"atm.name"你的余额:"atm.balance);while (true) {// 循环取款存款方法System.out.println("1.取款 2.存款 3.查询 4.退出");int option = sc.nextInt();if (option == 1) {System.out.println("输入取款金额");int money = sc.nextInt();atm.DrawMoney(money);// break;} else if (option == 2) {System.out.println("输入存款金额");int money1 = sc.nextInt();atm.SaveMoney(money1);// break;} else if (option == 3) {System.out.println("当前余额为:"atm.balance);// break;} else if (option == 4) {System.out.println("谢谢使用");System.exit(0);//退出jvm虚拟机} else {System.out.println("你输入的有误");}}} else {i--;// 每次输入错误就这里减一次if (i == 0) {// 判断等于0就执行里面的语句System.out.println("你的账号被冻结");break;// 输入密码循环结束}System.out.println("你输入的密码有误,还有"i"机会");}}}}
源文地址:https://www.guoxiongfei.cn/csdn/5367.html