zoukankan      html  css  js  c++  java
  • 课堂例子

    【课堂例子】

    1运行结果截图:

    使用instanceof运算符判断一个对象是否可以转换成指定的类型

    2运行结果截图:

    Dog和cat类均为animal的子类,所以dog和cat均可以转化为animal类,而animal类不可以直接转化为dog和cat类

    3运行结果截图:

           4运行结果截图:

          5运行结果截图:

             6运行结果截图:

    7运行结果:Exception thrown in method3

     

    java.lang.Exception: Exception thrown in method3

        at com.TestInstanceof.method3(TestInstanceof.java:29)

        at com.TestInstanceof.method2(TestInstanceof.java:24)

        at com.TestInstanceof.method1(TestInstanceof.java:19)

        at com.TestInstanceof.main(TestInstanceof.java:9)

    8 ThrowMultiExceptionsDemo.java的

    运行结果:这只是一个测试

    捕捉异常

    9ExceptionLinkInRealWorld的运行结果:Exception handled in method throwException

    Finally executed in throwException

    在方法执行时出现异常

    系统运行时引发的特定的异常

    Method doesNotThrowException

    Finally executed in doesNotThrowException

    End of method doesNotThrowException

    动手动脑:

    代码:package com;

    import java.util.*;

    public class ClassGrade {

       

        public static void main(String[] args) {

               // TODO Auto-generated method stub

    try

    {

          Scanner sc=new Scanner(System.in);

          System.out.println("输入a:");

          int a=sc.nextInt();

         

               if(a<0||a>100)

               {

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

          a=sc.nextInt();

    }

    else

          if(a<60)

               System.out.println("这门课的成绩为不及格:");

       if(a>=60&&a<70)

             System.out.println("这门课的成绩为及格");

        

       if(a>=70&&a<80)

             System.out.println("这门课的成绩为中:");

       if(a>=80&&a<90)

             System.out.println("这门课的成绩为良:");

       if(a>=90&&a<100)

             System.out.println("这门课的成绩为优:");

          }

              

     

          catch(InputMismatchException e){

               System.out.println("输入必须为整数");

          }

    }

    }

     

    运行结果截图:输入a:

    30

    这门课的成绩为不及格:

  • 相关阅读:
    Qt 读写XML文件
    用 Qt 中的 QDomDocument类 处理 XML 文件(上)
    Qss
    QTableWidget的使用和美工总结
    用 Qt 中的 QDomDocument类 处理 XML 文件(下)
    ArcEngine中最短路径的实现
    AE中网络分析的实现 的各个类之间的关系
    AE控制图层中要素可见状态的几种方法
    如何使用Name对象,包括WorkspaceNames和DatasetNames
    AE属性表操作
  • 原文地址:https://www.cnblogs.com/19950216z/p/4967334.html
Copyright © 2011-2022 走看看