第六次上机

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.AncestorListener;

public class Mystcolor implements ActionListener {
JFrame j;
JButton b;
JPanel p;
JLabel l;
public Mystcolor(){
j=new JFrame("改变颜色");
b=new JButton("颜色按钮");
p=new JPanel();
b.addActionListener(this);
l=new JLabel("按钮");
p.add(l);
p.add(b);
j.add(p);
j.setSize(400,400);
j.setLocation(100, 100);
j.setVisible(true);
}

public static void main(String[] args) {
	new Mystcolor();

}

@Override
public void actionPerformed(ActionEvent e) {
	p.setBackground(Color.blue);
	
}

}

import java.util.Scanner;

public class ExceptionTest {

public static void main(String[] args) {
	Scanner er=new Scanner(System.in);
	int c;
	int a=er.nextInt();
	int b=er.nextInt();
	try{
		c=a/b;
		System.out.println(c);
	}catch(Exception e){
		System.out.println("被0除所产生的异常");
		e.printStackTrace();
	}
	finally{
		System.out.println(a);
	}

}

}
1
0
被0除所产生的异常
1
java.lang.ArithmeticException: / by zero
at ExceptionTest.main(ExceptionTest.java:11)

import java.util.Scanner;

public class doublesttd {

public static void main(String[] args) {
	Scanner er=new Scanner(System.in);
	double r;
	try{
		r=er.nextDouble();
		double s;
		s=3.14*r*r;
		System.out.println("次圆面积为:"+s);
	}catch(Exception e){
		System.out.println("此输入数不为double型");
		e.printStackTrace();
	}
	
}

}
ww
java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at doublesttd.main(doublesttd.java:9)
此输入数不为double型

import java.util.Scanner;

public class identify {
static void the() throws IllegalArgumentException{
throw new IllegalArgumentException();
}

public static void main(String[] args) {
	Scanner er=new Scanner(System.in);
	String id=er.nextLine();
	String ide;
	try{
		if(id.length()==18){
			ide=id;
			System.out.println(ide);
		}
		else{
			the();
		}
	}catch(IllegalArgumentException e){
		System.out.println("不符合身份证号");
	}
	
}

}
7536452646246754673156456546164
不符合身份证号

posted @ 2019-05-09 18:30  龙吕  阅读(138)  评论(0编辑  收藏  举报