• 异常


    import java.util.*;
    public class ScoreRank {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            Scanner in=new Scanner(System.in);
            System.out.println("请输入学生的成绩(100以内整数)");
            String score=in.next();
            int flag=1;
            try 
            {
                if(score.length()>2)
                {
                    if(!score.equals("100"))
                        flag=0;
                }
                if(score.charAt(0)=='0')
                {
                    flag=0;
                }
                for(int i=1;i<score.length();i++)
                {
                    if(score.charAt(i)>'9'||score.charAt(i)<'0')
                    {
                        flag=0;
                    }
                }
                if(flag==0)
                {
                    MyException e=new MyException("非法输入(不符合格式或包含非数字项或超出限制)");
                    throw e;
                }
            
            }
            catch(MyException e)
            {
                System.out.println(e);
            }
            //成绩评判
            if(flag==1)
            {
                int s=Integer.parseInt(score);
                if(s<60)
                    System.out.println("不及格");
                else 
                {
                    System.out.println("及格");
                    if(s>60&&s<=70)
                        System.out.println("中");
                    if(s>70&&s<=80)
                        System.out.println("良");
                    if(s>80)
                        System.out.println("优");
                }
            }
        }
    
    }
    class MyException extends Exception
    {
        MyException(String x)
        {
            super(x);
        }
    }

    运行结果截图:

    import java.util.*;
    public class ATMRe {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            //如果使用的是Account类数组,可以使用以下方式直接add添加
            //目前其中只有获得余额的方法
            Vector<Information> ac=new Vector<Information>();
            
            Scanner in=new Scanner(System.in);
            Manage test=new Manage("Tom","6212260402013456234","123456",1000);
            int c1,c2;
            System.out.println("请输入密码");
            int flag=0;
            try
            {
                int error=0;
                for(int i=0;i<3;i++)
                {
                    String p=in.next();
                    if(!p.equals(test.getpassword()))
                    {
                        error++;
                    }
                    else
                    {
                        flag=1;
                        break;
                    }
                }
                    if(error==3)
                    {
                        PasswordException e1=new PasswordException("输入密码错误,银行卡被锁死");
                        throw e1;
                    }
            }
            catch(PasswordException e)
            {
                System.out.println(e);
            }
            
            if(flag==1)
            {
                while(true)
                {
                    test.window();
                    System.out.println("请选择");
                    c1=in.nextInt();
                    test.setchoice(c1);
                    if(test.getchoice()==1)
                    {
                        System.out.println("请输入存款金额");
                        double x=in.nextDouble();
                        try
                        {
                            if(x%100!=0)
                            {
                                DepositException e2=new DepositException("输入钱数不是100的整数");
                                throw e2;
                            }
                            test.deposit(x);
                        }
                        catch(DepositException e2)
                        {
                            System.out.println(e2);
                        }
                        System.out.println("用户余额为"+test.getbalance()+"元");
                    }
                    if(test.getchoice()==2)
                    {
                        test.withdrawwindow();
                        System.out.println("请选择");
                        c2=in.nextInt();
                        if(c2==8)
                            break;
                        else
                        {
                            test.withdraw(c2);
                        }
                    }
                    if(test.getchoice()==3)
                    {
                        System.out.println("请输入转账卡号(19位数字)、转账金额");
                        String n=in.next();
                        double m=in.nextDouble();
                        if(m>test.getbalance())
                        {
                            System.out.println("转账金额超出余额,转账失败");
                        }
                        else
                        {
                        try
                        {
                            if(n.length()>19||n.length()<19)
                            {
                                TransferException e3=new TransferException("非法输入(银行卡号输入错误)");
                                throw e3;
                            }
                            for(int i=0;i<19;i++)
                            {
                                if(n.charAt(i)<'0'||n.charAt(i)>'9')
                                {
                                    TransferException e3=new TransferException("非法输入(银行卡号输入错误)");
                                    throw e3;
                                }
                            }
                            test.transfer(n, m);
                        }
                        catch(TransferException e3)
                        {
                            System.out.println(e3);
                        }
                        
                        }
                        System.out.println("用户余额为"+test.getbalance()+"元");
                        
                    }
                    if(test.getchoice()==4)
                    {
                        System.out.println("请输入修改的密码(6位数字)");
                        String z=in.next();
                        
                        try
                        {
                            if(z.length()>6||z.length()<6)
                            {
                                RePasswordException e4=new RePasswordException("非法输入(密码位数错误限制或包含非数字项)");
                                throw e4;
                            }
                            for(int i=0;i<6;i++)
                            {
                                if(z.charAt(i)>'9'||z.charAt(i)<'0')
                                {
                                    RePasswordException e4=new RePasswordException("非法输入(密码位数错误或包含非数字项)");
                                    throw e4;
                                }
                            }
                            test.setpassword(z);
                        }
                        catch(RePasswordException e4)
                        {
                            System.out.println(e4);
                        }
                        
                        if((test.getpassword()).equals(z))
                            System.out.println("修改成功");
                        else 
                        {
                            System.out.println("修改失败,请重新选择修改密码操作");
                        }
                    }
                    if(test.getchoice()==5)
                    {
                        System.out.println("用户余额为"+test.getbalance()+"元");
                    }
                }
            }
            else
            {
                System.out.println("用户输入错误密码,该卡已被锁定,无法操作!");
            }
        }
    
    }
    class Information
    {
        public void outputbalance(Vector<Account> ac)
        {
            for(Account a:ac)
            {
                a.getbalance();
            }
        }
    }
    class Manage extends Account
    {
        Manage(String n,String i,String p,double b)
        {
            name=n;
            id=i;
            password=p;
            balance=b;
        }
        public void window()
        {
            System.out.println("************");
            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("************");
        }
        public void withdrawwindow()
        {
            System.out.println("************");
            System.out.println("取款窗口");
            System.out.println("1.100");
            System.out.println("2.500");
            System.out.println("3.1000");
            System.out.println("4.1500");
            System.out.println("5.2000");
            System.out.println("6.5000");
            System.out.println("7.其他金额");
            System.out.println("8.退卡");
            System.out.println("9.返回");
            System.out.println("************");
        }
        public void withdraw(int c)
        {
            choice=c;
            if(choice==1) 
            {
                    if(balance-100>=0)
                        balance=balance-100;
                    else
                    {
                        System.out.println("余额不足,取款失败");
                        window();
                    }
            }
            if(choice==2) 
            {
                if(balance-500>=0)
                    balance=balance-500;
                else
                {
                    System.out.println("余额不足,取款失败");
                    window();
                }
            }
            if(choice==3) 
            {
                if(balance-1000>=0)
                    balance=balance-1000;
                else
                {
                    System.out.println("余额不足,取款失败");
                    window();
                }
            }
            if(choice==4) 
            {
                if(balance-1500>=0)
                    balance=balance-1500;
                else
                {
                    System.out.println("余额不足,取款失败");
                    window();
                }
            }
            if(choice==5) 
            {
                if(balance-2000>=0)
                    balance=balance-2000;
                else
                {
                    System.out.println("余额不足,取款失败");
                    window();
                }
            }
            if(choice==6) 
            {
                if(balance-5000>=0)
                    balance=balance-5000;
                else
                {
                    System.out.println("余额不足,取款失败");
                    window();
                }
            }
            if(choice==7)
            {
                System.out.println("请输入存款金额");
                double x=input.nextDouble();
                    if(balance>=x)
                        balance=balance-x;
                    else
                    {
                        System.out.println("余额不足,取款失败");
                        window();
                    }
                    
            }
            if(choice==9)
                window();
        }
        public void deposit(double d)
        {
            balance=balance+d;
        }
        public void transfer(String a,double t)
        {
            if(balance>=t)
            {
                balance=balance-t;
                System.out.println("转账成功");
            }
            else
            {
                System.out.println("余额不足,无法转账");
            }
        }
    }
    class Account
    {
        Scanner input=new Scanner(System.in);
        public String id;
        public String name;//姓名
        public int choice;//操作
        public String password;//密码
        public double balance;//余额
        Account(){}
        public double getbalance()
        {
            return balance;
        }
        public void setchoice(int c)
        {
            choice=c;
        }
        public int getchoice()
        {
            return choice;
        }
        public String getpassword()
        {
            return password;
        }
        public void setpassword(String p)
        {
            password=p;
        }
    }
    class PasswordException extends Exception
    {
        PasswordException(String x)
        {
            super(x);
        }
    }
    class DepositException extends Exception
    {
        DepositException(String x)
        {
            super(x);
        }
    }
    class RePasswordException extends Exception
    {
        RePasswordException(String x)
        {
            super(x);
        }
    }
    class TransferException extends Exception
    {
        TransferException(String x)
        {
            super(x);
        }
    }

    运行结果截图:

     

     

    当在捕获到异常的处理代码里加入system.exit()时,finally的代码块是不会执行的。捕获异常的规则是尽量优先捕获具体的异常。

    finally 块:无论是否捕获或处理异常,finally块里的语句都会被执行。当在try块或catch块中遇到return语句时,finally语句块将在方法返回之前被执行。在以下4种特殊情况下,finally块不会被执行:
    1)在finally语句块中发生了异常。
    2)在前面的代码中用了System.exit()退出程序。
    3)程序所在的线程死亡。
    4)关闭CPU。

  • 相关阅读:
    解决win8无法成功安装Windows Phone 7 sdk的问题
    决定专心写博,学习
    时间管理的首要原则:专注力
    Windows Phone 7 开发环境的搭建
    Windows Phone 8 开发环境的搭建
    学习使用ErrorProvider 转载
    SetTimer函数
    网络工程课程笔记
    IP地址分类及特殊IP地址
    windows消息处理机制
  • 原文地址:https://www.cnblogs.com/clueless/p/6099718.html
Copyright © 2020-2023  润新知