代码如下:
package kehouzuoye26_2;
public class QuQianException extends RuntimeException{
public QuQianException(){}
public QuQianException(String s){
super (s);
}
}
package kehouzuoye26_2;
import java.util.Scanner;
public class QuQianExceptionDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n= 10000;
w:while(true){
System.out.println("请输入取款金额:");
int a = sc.nextInt();
if(a<=n){
System.out.println("取款金额为:"+a);
n = n-a;
System.out.println("继续取款请输入 “1” 结束取款请输入“2”");
int c = sc.nextInt();
sc.nextLine();
switch (c) {
case 1:
break;
case 2:
System.out.println("卡上余额为"+n);
break w;
default:
System.out.println("选择输入有误");
}
}else{
throw new QuQianException("余额不足");
}
}
}
}
|
|