有这样一个题目:首先自定义一个异常,然后用户输入10个数整型,若不为整数则抛出自定义异常,并且将此数置0。然后将这10个数相加
如果总数>1000,则抛出自定义异常,并且将总和写入磁盘文件(在自定义异常中做)。
不会做的 哪位高手帮忙一下 多谢拉

解决方案 »

  1.   

    发个思路给你 , 仅作参考package com.question;import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Q5 {
        private static final int MAX = 1000;
        
        @Deprecated
        public static void main(String[] args) {
            args = new String[]{"1", "2200"};
            Q5 q5 = new Q5();
            try {
                q5.check(args);
            } catch (OutOf1000Excetion e) {
                e.printStackTrace();
            } catch (NotIntegerException e) {
                e.printStackTrace();
            }
        }
        
        /**
         * 
         * 
         * @param strs
         * @throws NotIntegerException
         * @throws OutOf1000Excetion
         */
        public void check(String... strs) throws NotIntegerException, OutOf1000Excetion {
            Pattern pattern = Pattern.compile("\\d+");
            int count = 0;
            for (int i = 0; i < strs.length; i ++) {
                Matcher matcher = pattern.matcher(strs[i]);
                if (!matcher.matches()) {
                    throw new NotIntegerException("Not Integer");
                }
                count = count + Integer.parseInt(strs[i]);
                if (count > MAX) {
                    throw new OutOf1000Excetion("Too big, large 1000");
                }
            }
        }
        
        /**
         * 不为整数则抛出自定义异常
         * 
         * @author cong_px
         */
        class NotIntegerException extends Exception {
            public NotIntegerException() {
                super();
            }
            
            /**
             * 
             * @param message 
             */
            public NotIntegerException(String message) {
                super(message);
            }
        }
      
        /**
         * 总数>1000,则抛出自定义异常
         *
         */
        class OutOf1000Excetion extends Exception {
            
            /**
             * 
             */
            public OutOf1000Excetion() {
                super();
            }
            
            /**
             * 
             * @param message
             */
            public OutOf1000Excetion(String message) {
                super(message);
            }
        }
    }
      

  2.   


    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;public class ExceptionTest {
    private static final int MAX = 1000; public static void main(String[] args) throws OutOf1000Excetion {
    int[] inputData = getInputData();

    int sum = 0;
    for(int i = 0; i < inputData.length; i++)
    sum += inputData[i];
    if(sum > 1000)
    throw new OutOf1000Excetion(sum, "Out of 1000");
    System.out.println("Sum=" + sum);
    } private static int[] getInputData() {
    int size = 10;
    int ret[] = new int[size];
    DataInputStream dis = new DataInputStream(System.in);
    String temp = null;
    for (int i = 0; i < 10; i++) {
    System.out.println("please enter integer[" + i + 1 + "]:");
    try {
    temp = dis.readLine();
    if (!temp.matches("[+-]?\\d+")) {
    temp = "0";
    throw new NotIntegerException("Not Integer.");
    }
    } catch (Exception e) {
    }
    ret[i] = Integer.parseInt(temp);
    }
    return ret;
    }
    }class NotIntegerException extends Exception {
    public NotIntegerException() {
    super();
    } public NotIntegerException(String message) {
    super(message);
    }
    }class OutOf1000Excetion extends Exception { public OutOf1000Excetion(int sum, String message) {
    super(message);
    writeToDisk(sum);
    }

    private void writeToDisk(int sum)
    {
    DataOutputStream dos = null;
    try {
    dos = new DataOutputStream(new FileOutputStream("D:\\input.txt"));
    dos.write(String.valueOf(sum).getBytes());
    dos.flush();
    dos.close();
    } catch (Exception e) {
    e.printStackTrace();
    if(dos!=null)
    try {
    dos.close();
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    return;
    }
    }
    }