第六次实训作业

package bbb;
import java.util.Scanner;

public class ExceptionTest {
	public static void main(String[] args){
		try{
			double c;
			Scanner a=new Scanner(System.in);
			int A;
			A=a.nextInt();
			Scanner b=new Scanner(System.in);
			int B;
			B=b.nextInt();
			c = A/B;
			System.out.println(c);
		}catch(ArithmeticException e){
			 System.out.println("除数不能为零!");
		}
		System.out.println("program over");
	}

}

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

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

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

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

 

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

package bbb;
import java.util.Scanner;
public class ExceptionTest {
	public static void main(String[] args){
		try{
			 double m;
			        Scanner R=new Scanner(System.in);
			          int r;
			       r=R.nextInt();
			      m=r*r*3.14;
	        System.out.println("面积为:"+m);
		}catch(Exception e){
			 System.out.println("半径要为double类型!");
		}
		System.out.println("program over");
		}

		}
	

 

posted @ 2019-07-05 10:42  就很棒!  阅读(135)  评论(0)    收藏  举报