异常处理-三角形

import java.util.Arrays;  
import java.util.Scanner;  
import java.util.InputMismatchException;  

public class TestTriangle {
	public static void triangle(int a, int b,int c) throws IllegalArgumentException, InputMismatchException{  
                   int x[] = new int[3];  
	                x[0] = a;  
		               x[1] = b;  
		                x[2] = c;  
		                Arrays.sort(x);  
		                if ((x[0]+x[1]>x[2])&&(x[2]-x[1]<x[0]))  
		                    System.out.println("三角形的三边长为:"+a+","+b+","+c);  
		                else  
		                    throw new IllegalArgumentException();  
		            }  
		  
		            public static void main(String[] args) {  
		                int a=0, b=0, c=0;  
		                Scanner in = new Scanner(System.in);  
		                System.out.println("请分别输入三角形的三边长:");  
		                try{  
		                  a = in.nextInt();  
		                    b = in.nextInt();  
		                    c = in.nextInt();  
		                    triangle(a, b, c);  
		                }catch(InputMismatchException e1){  
		                    System.err.println("请输入整数作为三角形的边长!");  
		                    e1.printStackTrace();  
		                }catch(IllegalArgumentException e2){  
		                    System.err.println(a+","+b+","+c+"不能构成三角形");  
		                }  
		            }  

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值