1.Account类
package lianxi; public class Account { String zhanghao; double cunkuanyu_e; Account (String hao,double a) { zhanghao=hao; cunkuanyu_e=a; } //存款之后 并得到余额 //有返回值带参数的方法 double cunkuan(Double cunqian) { if(cunqian<0) { System.out.println("钱数错误"); } else { cunkuanyu_e+=cunqian; System.out.println("存钱"+cunqian+"元"); } return cunkuanyu_e; } void qukuan(Double quqian) { //验证 if(quqian>cunkuanyu_e) { System.out.println("余额不足"); return; } cunkuanyu_e-=quqian; System.out.println("取钱"+quqian+"元"); } //有返回值 无参数的方法 double yu_echaxun() { System.out.println("帐号 "+zhanghao+" 余额为 "+cunkuanyu_e+"元"); return cunkuanyu_e; } }
2.main
package lianxi; public class Ceshi { public static void main(String[] args) { Account yinhang=new Account("1234567890123", 20000.0); yinhang.cunkuan(500.0);
yinhang.yu_echaxun(); yinhang.qukuan(700.0); yinhang.yu_echaxun(); } }