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("结束");
            }
        }
    }

  • 相关阅读:
    SQLite 的连接串
    输入数组长度大于此表中的列数
    MVC3.0入门学习笔记页面传值ViewData
    MVC3.0入门学习笔记页面传值TempData
    在控制台程序中显示进度
    WCF学习笔记(1) 一个简单的wcf实例
    webBrowser 操作无ID元素
    webBrowser 设置文本框
    cookie总结
    checkbox是否被选择
  • 原文地址:https://www.cnblogs.com/luli1220/p/10830588.html
Copyright © 2011-2022 走看看