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

    import java.util.Scanner;
    
    public class ScoreRange {
        public static void main(String[] args) {
            System.out.println("请输入成绩");
            Scanner sc = new Scanner(System.in);
            String ss = sc.next(); //将数据以String类型的方式读入
            if(isInt(ss)==false) {
                System.exit(0);
            }
            else {
                int score = Integer.valueOf(ss);
                if(score<0||score>100) {
                    System.out.println("您输入的数据非法,请重新输入");
                }
                else if(score>=0&&score<60) {
                    System.out.println("不及格");
                }
                else if(score>=60&&score<70) {
                    System.out.println("及格");
                }
                else if(score>=70&&score<80) {
                    System.out.println("中等");
                }
                else if(score>=80&&score<90) {
                    System.out.println("良好");
                }
                else {
                    System.out.println("优秀");
                }
                sc.close();
            }
        }
        public static boolean isInt(String ss) {
            Integer it = null;
            try {
                it = Integer.valueOf(ss);
            } catch (NumberFormatException e) {
                System.out.println("您输入的数据不合法,请重新输入");
                return false;
            }
            return true;
        }
    }

    截图:

  • 相关阅读:
    HIVE(2) 之 常用函数
    HIVE的Shell操作
    HIVE常用函数(1)聚合函数和序列函数
    版本控制系统之SVN和GIT的区别
    【PyQt5】信号与槽+装饰器定义槽函数
    【PyQt5】信号与槽+多线程
    【PyQt5】信号与槽用法进阶
    【PyQt5】信号与槽用法入门
    【python之路】【5、函数学习】带装饰器的函数作业【老男孩第2期全栈】
    【python之路】【4、文件操作】数据类型、文件操作-作业 用文件储存三级菜单并执行
  • 原文地址:https://www.cnblogs.com/tiantain1015/p/4963205.html
Copyright © 2011-2022 走看看