java账户余额添加怎么处理_java 创建账户类,定义存钱取钱,查询余额方法,并自定义两个异常 取出比账户上多的钱 当小于0的钱存入...

packagech1;importjava.util.Scanner;publicclasscount{intmoney;intcountmoney;publiccount(){money=0;countmoney=500;}publicintgetMoney(){returnmoney;}publicvoidsetMoney(intmo...

package ch1;

import java.util.Scanner;

public class count {

int money;

int countmoney;

public count(){

money=0;

countmoney=500;

}

public int getMoney() {

return money;

}

public void setMoney(int money) {

this.money = money;

}

public int getCountmoney() {

return countmoney;

}

public void setCountmoney(int countmoney) {

this.countmoney = countmoney;

}

public void inMoney(int t)throws InvalidDepositException{

if(t<0) {

throw new InvalidDepositException();

}

}

public void outMoney(int m) throws AccountOverdrawnException{

if(m>=countmoney) {

throw new AccountOverdrawnException();

}

}

public void showMoney() {

System.out.println("余额为:"+countmoney);

}

public static void main(String[] args) {

System.out.println("请选择:1.存钱 2.取钱 3.查看余额 4.取消");

Scanner sc=new Scanner(System.in);

int choice=sc.nextInt();

count text=new count();

switch(choice){

case'1':

System.out.println("请输入存钱数目:");

Scanner sc2=new Scanner(System.in);

int b=sc2.nextInt();

try {

text.inMoney(b);

System.out.println("存了"+b+"元");

text.setMoney(b);

text.setCountmoney(500+b);

}

catch(InvalidDepositException e){

System.out.println(e.getMessage());

}

break;

case'2':

System.out.println("请输入取钱数目:");

Scanner sc1=new Scanner(System.in);

int a=sc1.nextInt();

try {

text.outMoney(a);

System.out.println("取了"+a+"元");

text.setMoney(a);

text.setCountmoney(500-a);

}

catch(AccountOverdrawnException e){

System.out.println(e.getMessage());

}

break;

case'3':

text.showMoney();

break;

case'4':System.out.println("取消");

break;

}

}

}

无错误,但是运行不了,输入选择之后就没有了....

展开

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值