阅读以下代码(CatchWho.java),写出程序运行结果:

 

运行结果:

 

写出CatchWho2.java程序运行的结果

 

运行结果:

 

请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。

 

当有多层嵌套的finally时,异常在不同的层次抛出  ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

编写一个程序,此程序在运行时要求用户输入一个   整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。

源代码:

import java.util.Scanner;

 

public class Chengji {

 

    public static void main(String[] args) {

        boolean f=true,h=true,q=true;

        int Sc=0;

      while(f){

        System.out.println("请输入成绩:");

        Scanner s=new Scanner(System.in);

        String ss=s.nextLine();

      

        int a=ss.length();

        for(int i=0;i<a;i++){

            if((ss.charAt(i)<'0')||(ss.charAt(i)>'9'))

                 break;

               

            else

                    {

                     q=false;

                     h=false;

                    }

        }

       

       

        if(q){

        try{

            throw new ScException();

        }

        catch(ScException e){

            System.out.println("格式错误");

          }

        }

       

       

       if(!h)

       {

         Sc=Integer.parseInt(ss);

        if((Sc<0)||(Sc>100))

        {

            try{

                ScException x= new ScException();

                throw x;

            }

            catch(ScException x){

                System.out.println("输入异常,请输入整数");

           

               

            }

        }

        else

            f=false;

       }

      

      }

        if(Sc<60)

            System.out.println("不及格");

        else if(Sc<70)

            System.out.println("及格");

        else if(Sc<80)

            System.out.println("中");

        else if(Sc<90)

            System.out.println("良");

        else if((Sc<100)||(Sc==100))

            System.out.println("优");

            

    }

 

}

class ScException extends Exception{

    public ScException(){

       

    }

}

运行结果截图:

 

 

 

 

 

 

 

 

posted on 2016-12-10 21:02  胡泽杰  阅读(192)  评论(0编辑  收藏  举报