JAVA programming sixth time job

  1. The ".id ID number" attribute class setting value, when the value of the length to 18, assigned to the id, when the value of the length is not 18, an IllegalArgumentException exception, then the exception capture and processing, to achieve the above programming Features.

class ID {public
String A;
public void the setId (String ID) {
IF (id.length () == 18) {
this.a = A;
} {the else
the throw new new an IllegalArgumentException ( "length of ID number should be 18! ' );
}
}

public static void main(String[] args) { id ac = new id (); try { ac.setId("0123456789123456789"); } catch (IllegalArgumentException ie) { System.out.println(ie.getMessage()); } }

}

  1. Write an application that requires input from the keyboard radius of a circle of a double type, and outputs the calculated area. When the input test data is not double data type (such as string "abc") will produce any results, how to deal with.

import java.util.;
public class yuan {
public static void main(String[] args){
double a,p=3.14f,s=0;
Scanner sc = new Scanner(System.in);
try{
a=sc.nextDouble();
s=aa*p;
}
catch(InputMismatchException e){

        System.out.print("输入错误!"); } System.out.print(s); }

}

  1. Write a class ExceptionTest, the main method used in the try-catch-finally statement structure to achieve:
    ² in the try block, divided by the number two write operations, the division requires two operands runtime user input;
    ² in catch block, the other exception is generated by the capture of 0, and outputs the abnormality information;
    ² in the finally block, the output of a statement.

import java.util.*;
public class take {
public static void main(String[] args){

    int a,b,c=0; Scanner ac= new Scanner(System.in); System.out.print("请输入被除数:"); a=ac.nextInt(); System.out.print("请输入除数:"); b=ac.nextInt(); try{ c=a/b; } catch(ArithmeticException e){ System.out.print("除数不能为0!"); } System.out.print("\n"); System.out.print(c); }

}

Guess you like

Origin www.cnblogs.com/abcd609553/p/11139863.html