import java.util.*;
/** 数据范围越界异常处理*/
class ShuzhiFanWeiException extends Exception
{
ShuzhiFanWeiException(String msg)
{
super(msg);
}
}
class Method
{
public static int[] Scanf() throws ShuzhiFanWeiException,InputMismatchException //数值输入方法
{
int arr[] = new int[3];
Scanner input = new Scanner(System.in);
for(int i=0;i<3;i++)
{
try
{
arr=input.nextInt();
}
catch (InputMismatchException e)
{
throw new InputMismatchException("不能输入非整数");
}
if(arr>40 || arr<-40)
{
throw new ShuzhiFanWeiException("数据超出给定的范围");
}
}
return arr;
}
public static int Add(int[] arr) //数值相加方法
{
int sum=0;
for(int i=0;i<3;i++)
{
sum+=arr;
}
return sum;
}
}
class TestDemo
{
public static void main(String[] args)
{
int arr[] = new int[3];
boolean state = true;
int sum=0;
while(state)
try
{
System.out.println("请输入三个-40至40的整数");
arr=Method.Scanf();
sum=Method.Add(arr);
state=false;
}
catch (InputMismatchException e)
{
System.out.println(e.getMessage());
}
catch (ShuzhiFanWeiException e)
{
System.out.println(e.getMessage());
}
System.out.println(sum);
}
} |
|