首页 > 代码库 > 异常处理

异常处理

 

 

import java.util.*;

public class ATM1 {

 @SuppressWarnings("resource")

    public static void main(String[] args) {

      Scanner in=new Scanner(System.in);

       ATM atm=new ATM();

       int choose=0,num=0;

       String pw="";

       next:while(true){

           System.out.println("是否进入账户(01是):");

           int kk=in.nextInt();

           if(kk==0) break;

            else if(kk!=1){

           System.out.println("输入错误!");

             continue;

                         }

           System.out.println("输入账户密码:");

           pw=in.next();

           if(atm.ifPass(pw)){

               while(true){

             showFace();

             choose=in.nextInt();

              switch(choose){

              case 1:

            System.out.println("输入存款金额:");

            num=in.nextInt();

            

            atm.save(num);

            System.out.println("存款成功!");

             System.out.println("当前余额:"+atm.getRest()+"");

             break;

             case 2:

 

              System.out.println("请选择:");

              int a[]={100,500,1000,1500,2000,5000};

              for(int i=0;i<a.length;i++)

              System.out.println((i+1)+"."+a[i]+"");

 

              System.out.println("7.其他");

 int ch=in.nextInt();

 if(ch>=1&&ch<=6){

 if(atm.withdraw(a[ch-1]))

 System.out.println("取款成功!");

else

 System.out.println("余额不足!");

 }

else if(ch==7){

 System.out.println("请输入取款金额:");

num=in.nextInt();

 if(atm.withdraw(num))

 System.out.println("取款成功!");

else

 System.out.println("余额不足!");

 }

else

 System.out.println("输入有误!");

System.out.println("当前余额:"+atm.getRest()+"");

break;

 case 3:

 System.out.println("账户号:");

String s=in.next();

 System.out.println("转账金额:");

int i=in.nextInt();

 if(atm.transfer(s, i))

 System.out.println("转账成功!");

else

 System.out.println("转账失败!");

System.out.println("当前余额:"+atm.getRest()+"");

break;

 case 4:

 System.out.println("输入六位数密码:");

String p=in.next();

 atm.setPassword(p);

 break;

 case 5:

 System.out.println("当前余额:"+atm.getRest()+"");

break;

 default:

 continue next;

 }

 }

 }

 else

 System.out.println("密码错误!");

 }

 }

 //显示菜单方法

public static void showFace(){

 System.out.println("********************");

 System.out.println(" 1.存款:");

System.out.println(" 2.取款:");

System.out.println(" 3.转账汇款:");

System.out.println(" 4.修改密码:");

System.out.println(" 5.查询余额:");

System.out.println(" 6.退卡:");

System.out.println("********************");

 System.out.println("请选择:");

 }

}

 

class PersonalAccount{

 private String passWord="123456";

 @SuppressWarnings("unused")

 private String number;//银行卡号

private int money=0;

 public int getMoney(){return money;}

 public void setPw(String s){passWord=s;}

 public void addMoney(int x){money+=x;}

 public void minusMoney(int x){money-=x;}

 public boolean whetherPwTrue(String s){

 if(s.equals(passWord))

 return true;

 else return false;

 }

}

abstract class PATM{

 public abstract boolean withdraw(int x);

 public abstract void save(int x);

 public abstract boolean transfer(String s,int x);

 public abstract boolean ifPass(String s);

 public abstract int getRest();

 public abstract void setPassword(String s);//设置密码

}

class ATM extends PATM{

 private String numbers[]={"3235635","3256894",

 "3265894","6589458"};

 private PersonalAccount account=new PersonalAccount();

 public boolean withdraw(int x) {

 if(x>account.getMoney())

 return false;

 else{

 account.minusMoney(x);

 return true;

 }

 }

 public void save(int x) {

 account.addMoney(x);

 }

 public boolean transfer(String s, int x) {

 

 boolean flag=false;

 for(int i=0;i<numbers.length;i++)

 if(s.equals(numbers[i])) flag=true;

 if(x>account.getMoney()) flag=false;

 if(x<=account.getMoney()&&flag) account.minusMoney(x);;

 return flag;

 }

 public boolean ifPass(String s) {

 return account.whetherPwTrue(s);

 }

 public int getRest() {

 return account.getMoney();

 }

 public void setPassword(String s) {

 account.setPw(s);

 }

}

 

 

 

 

 

 

 

 

//判断数字并捕获可能出现的三种异常。

public class MyException{

public static void check(String str1)

throws NullPointerException{

//方法头抛出空指针异常  

if(str1.length()>2){

str1=null;

}

}

    System.out.println(str1.length());

//空字符串的长度

}

   char ch;

for (int i=0;i<str1.length();i++){

String str1;

ch=str1.charAt(i);

     if(!Character.isDigit(ch))

      //判断参数中字符是否为数字

   throw new NumberFormatException();  //方法中抛出数字格式异常   

  }

}

  public static void main(String args[])

throws Exception{

//抛出异常给系统处理    

int num;

try{

    check(args[0]);

     num=Integer.parseInt(args[0]);

     if (num>60)

    System.out.println("成绩为:"+num+"  及格");

    else

    System.out.println("成绩为:"+num+"  不及格");

}

   catch(NullPointerException e){

     System.out.println("空指针异常:"+e.toString());

}

    catch(NumberFormatException ex){

    System.out.println("输入的参数不是数值类型");

    }

     catch(Exception e){

    System.out.println("命令行中没有提供参数");

       }  

     }

    }

    //求阶乘并捕获可能出现的三种异常

    public class MyException{

     public static double multi(int n){

     if(n<0)

       throw new IllegalArgumentException("求负数阶乘异常");

       double s=1;

     for(int i=1;i<=n;i++)

     s=s*i;     

          return s;    

         }

     public static void main(String[] args){

          try{

          }

           int m=Integer.parseInt(arg[0]);

     System.out.println(m+"!="+multi(m));

      //调用方法multi求阶乘。      

      }

     catch (ArrayIndexOutOfBoundsException e){

     System.out.println("命令行中没提供参数!");      

      }

          catch (NumberFormatException e){

     System.out.println("应输入一个整数!");   

         }

      catch (IllegalArgumentException e){

     System.out.println("出现的异常是:"+e.toString());   

         } 

           finally{

     System.out.println("程序运行结束!");     

       }  

        }

     }

异常处理