zoukankan      html  css  js  c++  java
  • 课后作业

    一.下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么?

    m=d;

    d=m;

    d=(Dog)m;

    d=c;

    c=(Cat)m;

    d=m;和d=c;引起编译错误;

    原因:

    二.CatchWho.java

    public class CatchWho { 
        public static void main(String[] args) { 
            try { 
                try { 
                    throw new ArrayIndexOutOfBoundsException(); 
                } 
                catch(ArrayIndexOutOfBoundsException e) { 
                   System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch"); 
                }
     
                throw new ArithmeticException(); 
            } 
            catch(ArithmeticException e) { 
                System.out.println("发生ArithmeticException"); 
            } 
            catch(ArrayIndexOutOfBoundsException e) { 
               System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
            } 
        } 
    }

    三.CatchWho2.java

    public class CatchWho2 { 
        public static void main(String[] args) { 
            try {
                try { 
                    throw new ArrayIndexOutOfBoundsException(); 
                } 
                catch(ArithmeticException e) { 
                    System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 
                }
                throw new ArithmeticException(); 
            } 
            catch(ArithmeticException e) { 
                System.out.println("发生ArithmeticException"); 
            } 
            catch(ArrayIndexOutOfBoundsException e) { 
                System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
            } 
        } 
    }

    结果:ArrayIndexOutOfBoundsException/外层try-catch

    四.EmbededFinally.java

    public class EmbededFinally {
    
        
        public static void main(String args[]) {
            
            int result;
            
            try {
                
                System.out.println("in Level 1");
    
               
                 try {
                    
                    System.out.println("in Level 2");
      // result=100/0;  //Level 2
                   
                     try {
                       
                         System.out.println("in Level 3");
                          
                         result=100/0;  //Level 3
                    
                    } 
                    
                    catch (Exception e) {
                        
                        System.out.println("Level 3:" + e.getClass().toString());
                    
                    }
                    
                    
                    finally {
                        
                        System.out.println("In Level 3 finally");
                    
                    }
                    
                   
                    // result=100/0;  //Level 2
    
                
                    }
                
                catch (Exception e) {
                   
                     System.out.println("Level 2:" + e.getClass().toString());
               
                 }
                 finally {
                    
                    System.out.println("In Level 2 finally");
               
                 }
                 
                // result = 100 / 0;  //level 1
            
            } 
            
            catch (Exception e) {
                
                System.out.println("Level 1:" + e.getClass().toString());
            
            }
            
            finally {
               
    .             System.out.println("In Level 1 finally");
            
            }
        
        }
    
    }

    结果:

    in Level 1

    in Level 2

    in Level 3

    Level 3:class java.lang.ArithmeticException

    In Level 3 finally

    In Level 2 finally

    In Level 1 finally

    五.课后作业2

    package score;
    import java.io.BufferedReader;//
    import java.io.InputStreamReader;//
    public class Score {
    public static void main(String[] args){
        int num=110 ;
        String str ;
        BufferedReader brd=new BufferedReader(new InputStreamReader(System.in)) ;//构造方法
        while(true)
        {
            System.out.print("请输入数字:") ;
            try
            {
                str=brd.readLine() ;
                System.out.print("输入为:"+str);
                num=Integer.parseInt(str) ;
                
            }
            catch(Exception e)
            {
                System.out.println("	对不起,只能输入整数,请重新输入。") ;
            }
            
            
            if(num>100||num<0)
            {
                
                System.out.println("对不起,只能输入1-100整数,请重新输入。") ;
            }
            else break;
        }
        if(num<60)
            System.out.println("你输入的整数是: "+num+"成绩为:不及格") ;
        else if(num<70)
            System.out.println("你输入的整数是: "+num+"成绩为:及格") ;
        else if(num<80)
            System.out.println("你输入的整数是: "+num+"成绩为:良") ;
        else if(num<90)
            System.out.println("你输入的整数是: "+num+"成绩为:中") ;
        else System.out.println("你输入的整数是: "+num+"成绩为:优") ;
        
    }
    }

  • 相关阅读:
    EventLog实现事件日志操作
    可否控制<link type=text/css rel=stylesheet href=style.css>
    强制IE浏览器或WebBrowser控件使用指定版本显示网页2
    C#中的@符号
    C#运算符大全_各种运算符号的概述及作用
    调试时设置条件断点
    C语言:用字符读取流和输出流来读写入数据。(文本文件)
    建立完整的单向动态链表(包括初始化、创建、插入、删除、查找、销毁、输出)
    C语言:创建动态单向链表,创建完成后,输出每一个节点的数据信息。
    C语言:使用realloc函数对malloc或者calloc动态分配的内存大小进行扩展
  • 原文地址:https://www.cnblogs.com/hongniuke/p/4966273.html
Copyright © 2011-2022 走看看