A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

©   /  2014-8-14 12:36  /  20709 人查看  /  85 人回复  /   2 人收藏 转载请遵从CC协议 禁止商业使用本文

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);
        }
}

QQ图片20140815144638.jpg (61.25 KB, 下载次数: 88)

QQ图片20140815144638.jpg

评分

参与人数 1技术分 +2 收起 理由
滔哥 + 2

查看全部评分

回复 使用道具 举报
菜鸟一号 发表于 2014-8-15 14:51
import java.util.*;
/** 数据范围越界异常处理*/
class ShuzhiFanWeiException extends Exception

谢谢滔哥
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马