• 异常作业


       今天做的是“异常‘。

        

    package aaa;
    import java.util.Scanner;
    public class bbb {
    public static void main(String[] args) {
     int a=0;
     int b=0;
     Scanner sc=new Scanner(System.in);
     try {
      System.out.println("请输入被除数:");
      a=sc.nextInt();
      System.out.println("请输入除数:");
      b=sc.nextInt();
     }catch(Exception e)
     {
      System.out.println("输入不合法:");
      e.printStackTrace();
     }
     finally {
     if(b==0) {
      System.out.println("b的值不能为0");
     }
     else {
      System.out.println(a+"/"+b+"="+(a/b));
     }
    }
    }
    }
     
    package aaa;
    import java.util.Scanner;
    public class Circle {
    public static void main(String[] args) {
     double r;
     Scanner s=new Scanner(System.in);
     try {
      r=Double.parseDouble(s.next());
      System.out.println("面积是"+3.14*r*r);
     }
     catch(Exception e) {
      e.printStackTrace();
     }
     finally {
      r=Double.parseDouble(s.next());
      System.out.println("面积是"+3.14*r*r);
     }
    }
    }
    package aaa;
     import java.util.Scanner;
     public class Excep {
     static String id;      
     public void length(String id) throws IllegalArgumentException{
         if (id.length() == 18) { 
                  this.id = id;
                 System.out.println("身份证号长度正常");
     }                           
        else{
             throw new IllegalArgumentException("身份证号长度异常");                          
         }
     }
     public static void main(String[] args) {
           Scanner sc=new Scanner(System.in);
           Excep A=new Excep();
     try {
                System.out.println("请输入身份证号:");
                 A.id=sc.next();
                A.length(A.id);
     } catch (IllegalArgumentException e) {
             System.out.println("异常");
        
                    System.out.println(e.getMessage());
     }finally{
                 System.out.println("身份证号码为:"+id);
         }
         }
     }
  • 相关阅读:
    pip安装不成功的解决办法
    nginx常用的请求头参数和防DDOS攻击
    nginx报错:‘open too many files’解决
    nginx根据http_user_agent来拦截访问
    centos7安装logstash
    aws创建实例ec2时关联IAM
    docker清理多余的镜像和stop的容器
    理想
    +: indexed part-select
    Features Download Pricing Mind Maps Blog XMind的快捷键
  • 原文地址:https://www.cnblogs.com/rfvh/p/10835759.html
Copyright © 2020-2023  润新知