zoukankan      html  css  js  c++  java
  • 5

    1. 编写一个类ExceptionTest,在main方法中使用try-catch-finally语句结构实现:

    ²  在try语句块中,编写两个数相除操作,相除的两个操作数要求程序运行时用户输入;

    ²  在catch语句块中,捕获被0除所产生的异常,并输出异常信息;

    ²  在finally语句块中,输出一条语句。

    import java.util.*;
    public class ExceptionTest {
        int a;
        int b;
        double c;
        public static void main(String[] args) {
        ExceptionTest ll = new ExceptionTest();
        Scanner rd = new Scanner(System.in);
     try{
         System.out.println("请输入被除数");
         ll.a=rd.nextInt();
         System.out.println("请输入除数");
         ll.b=rd.nextInt();
         ll.c=ll.a/ll.b;
         System.out.println(ll.c);
     }catch(ArithmeticException e){
         e.printStackTrace();
        }
     finally{
        System.out.println("除数不能为零");
     }
        }
    }

    1. 编写一个应用程序,要求从键盘输入一个double型的圆的半径,计算并输出其面积。测试当输入的数据不是double型数据(如字符串“abc”)会产生什么结果,怎样处理。

    import java.util.*;
    public class Yuan {
    double a;
    double b;
     public static void main(String[] args) {
     Yuan x =new Yuan();
     Scanner rd =new Scanner(System.in);
     try{System.out.println("请输入圆的半径");
     x.a=rd.nextDouble();
     x.b=x.a*x.a*3.14;
     System.out.println(x.b);
     }
     catch(InputMismatchException e){
      e.printStackTrace();
     }
     finally{
      System.out.println("半径必须为double型数据,请输入");
      Scanner rc =new Scanner(System.in);
      x.a=rc.nextDouble();
      x.b=x.a*x.a*3.14;
      System.out.println("圆的面积为"+x.b);
     }
     }
    }

    1. 为类的属性“身份证号码.id”设置值,当给的的值长度为18时,赋值给id,当值长度不是18时,抛出IllegalArgumentException异常,然后捕获和处理异常,编写程序实现以上功能。

    import java.util.*;
    public class idcard {
        static int quotient(int size)throws IllegalArgumentException{
            if(size!=18) {
                throw new IllegalArgumentException("长度不是18!");
            }
            return size;
        }
        public static void main(String[] args) {
            Scanner rd=new Scanner(System.in);
            System.out.println("请输入身份证号码:");
            String id=rd.next();
            try {
                int size=id.length();
                quotient(size);
                System.out.println(id);
            }
            catch(IllegalArgumentException e) {
                e.printStackTrace();
            }
            finally {
                System.out.println("结束");
            }
        }
    }

  • 相关阅读:
    Android 自动化测试 Emmagee
    接口测试
    office2010
    MonkeyRecorder
    反编译android的apk
    基于标准库的string类实现简单的字符串替换
    C++中如何在顺序容器中删除符合特定条件的元素
    结合示例说明C++中const和指针结合时怎么理解
    C++中const使用注意要点(二)
    C++中const使用注意要点(一)
  • 原文地址:https://www.cnblogs.com/luli1220/p/10830588.html
Copyright © 2011-2022 走看看