代码改变世界

课堂动手动脑

2016-11-25 18:54  ovols  阅读(155)  评论(0编辑  收藏  举报

动手动脑

1.

当在捕获到异常的处理代码里加入system.exit()时,finally的代码块是不会执行的。

            

2.

Catchwho结果:

             

3.

Catchwho2结果:

             

程序:

程序代码:

import java.util.*;

class MyException extends Exception

{

    MyException(String x)

    {

        super(x);

    }

}

public class ScoreRank {

    public static void main(String[] args) {

        Scanner scan=new Scanner(System.in);

        System.out.println("请输入学生的成绩");

        String score=scan.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("优");

            }

        }

    }

 

}

运行截图: