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;
}
}
}
无错误,但是运行不了,输入选择之后就没有了....
展开