1、Account类
package LESSON5; public class Account { public long id; public int password; public String name; public double balance=2000; public Account(long id,String name){ this.id=id; this.name=name; } public void deposit(double money){ balance+=money; System.out.println("当前余额为:"+balance); } public void withdraw(double money){ if(money<=balance){ balance-=money; System.out.println("当前余额为:"+balance); } else{ System.out.println("余额不足"); } } }
2、主程序
package LESSON5; import java.util.*; public class exercise3 { public static void main(String[] args) { int num=3; Account acc=new Account(123456,"张三"); Scanner sc=new Scanner(System.in); System.out.println("欢迎来到自助银行"); while(true){ System.out.println("请输入密码"); int pwd=sc.nextInt(); if(pwd==123) { System.out.println(acc.name+"欢迎,您的账户为"+acc.id); while(true){ System.out.println("请选择:1存款 2取款 3查询余额 4退出"); int a=sc.nextInt(); if(a==1){ System.out.println("请输入存款金额"); double money=sc.nextDouble(); acc.deposit(money); } else if(a==2){ System.out.println("请输入取款金额"); double money=sc.nextDouble(); acc.withdraw(money); } else if(a==3){ System.out.println("余额为:"+acc.balance); } else if(a==4){ System.exit(0);//退出jvm } else{ System.out.println("输入错误"); break; } } }
else{ num--; if(num==0){ System.out.println("账户已冻结"); break; } System.out.println("密码错误,您还有"+num+"次机会"); } } } }
3、运行结果