package 异常;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
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+"不能构成三角形");
}
}
}
package 异常;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TestArray {
public static void main(String[] args) {
int a[] = new int[5];
System.out.println("请输入5个数:");
System.out.println("最后输入一个非数字结束输入操作。");
Scanner in = new Scanner(System.in);
try{
int i = 0;
while(in.hasNextDouble()){
a[i] = in.nextInt();
i++;
}
if(i<5)
throw new ArrayIndexOutOfBoundsException();
for(int j=0;j<5;j++)
System.out.print(a[j]+" ");
System.out.println();
}catch(InputMismatchException e1){
System.err.println("请输入整数作为数组元素!");
e1.printStackTrace();
}catch(ArrayIndexOutOfBoundsException e2){
System.err.println("请输入5个数!");
e2.printStackTrace();
}finally{
System.out.print("感谢使用本程序!");
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有