java_依次输入10个不同的整数,一行输入一个整数。 若输入的整数与已经输入的整数相同,则抛出SameIntegerException异常,提示继续输入。

版本1(探索版)

package experiment6.exp3;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/*依次输入10个不同的整数,一行输入一个整数。
若输入的整数与已经输入的整数相同,则抛出SameIntegerException异常,提示继续输入。
如:输入十个不同整数[10, 23, 39, 22, 48, 77, -2, 30, 8, 99]
*/
public class exp3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入10个不同的整数,一个数一行:");
        List<Integer> list = new ArrayList<>();

        for (int i = 0; list.size()<10; i++) {
            String line = scanner.nextLine();
            //line = scanner.nextLine();
            int tmp = Integer.parseInt(line);

            if (list.contains(tmp)) {
                try {
                    //list.add(tmp);
                    throw new SameIntegerException(tmp,list);
                } catch (SameIntegerException e) {
                    e.printStackTrace();
                    System.out.println("请重新输入:");
                }

            }else
            {
                list.add(tmp);
            }

        }//endFor
        System.out.println("输入的10个不同整数为:\n"+list);
    }//endMain
}
/*测试数据:
10
23
39
10

22
48
39

77
-2
48

30
8
-2

99

*/
package experiment6.exp3;

import java.util.ArrayList;
import java.util.List;

public class SameIntegerException extends Exception{
    int m;
    List<Integer> list=new ArrayList<>();
    public SameIntegerException(int n,List<Integer> argList){
        m=n;
        list=argList;
    }
    @Override
    public String getMessage() {
        return "输入的数"+m+"和已输入的数"+list+"有相同\n";

    }
}

改进版

package experiment6.exp3;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/*依次输入10个不同的整数,一行输入一个整数。
若输入的整数与已经输入的整数相同,则抛出SameIntegerException异常,提示继续输入。
如:输入十个不同整数[10, 23, 39, 22, 48, 77, -2, 30, 8, 99]
*/
public class exp3 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入10个不同的整数,一个数一行:");
        List<Integer> list = new ArrayList<>();
        AddDifferentNum obj=new AddDifferentNum();
        for (int i = 0; list.size() < 10; i++) {
            String line = scanner.nextLine();
            int tmp = Integer.parseInt(line);

           try{
               obj.add(list, tmp);
           }catch (SameIntegerException e){
               e.printStackTrace();//区别于getStackTrace();方法.
               System.out.println("请重新输入:");
           }

        }//endFor
        System.out.println("输入的10个不同整数为:\n"+list);

    }//endMain
}

package experiment6.exp3;

import java.util.List;

public class SameIntegerException extends RuntimeException{
    int m;
    List<Integer> list;
    public SameIntegerException(int n,List<Integer> argList){
        m=n;
        list=argList;
    }
    @Override
    public String getMessage() {
        return "输入的数"+m+"和已输入的数"+list+"有相同";//+"\n"

    }
}

package experiment6.exp3;

import java.util.List;
public class AddDifferentNum {
    public void add(List<Integer> list,int tmp)
    {
        if (list.contains(tmp)) {
            throw new SameIntegerException(tmp, list);
        } else {
            list.add(tmp);
        }

    }
}
/*
附上样例:(从Integer.parseInt()的源代码可以看出,throw抛出异常后,不是在本方法中立即catch.
而且,throw xxException 在方法没有用try套住(尽管一般都是RunningTimeException)
不过这样将try...catch在调用者中执行,在程序捕捉到第一个异常后就离开了(方法中还未执行的部分就来不及执行完整)
也就是说,看需求啦,到底要不要在方法中直接处理(套上try...catch).
当然,一般如果业务逻辑分的分配的清楚合理的话,是可以值抛出if(){throw}后,让main()方法去try catch.


public static int parseInt(String s, int radix)
                throws NumberFormatException
    {
        *
         * WARNING: This method may be invoked early during VM initialization
         * before IntegerCache is initialized. Care must be taken to not use
         * the valueOf method.
         *

        if (s == null) {
                throw new NumberFormatException("null");
                }

                if (radix < Character.MIN_RADIX) {
        throw new NumberFormatException("radix " + radix +
        " less than Character.MIN_RADIX");
        }

        if (radix > Character.MAX_RADIX) {
        throw new NumberFormatException("radix " + radix +
        " greater than Character.MAX_RADIX");
        }

        boolean negative = false;
        int i = 0, len = s.length();
        int limit = -Integer.MAX_VALUE;

        if (len > 0) {
        char firstChar = s.charAt(0);
        if (firstChar < '0') { // Possible leading "+" or "-"
        if (firstChar == '-') {
        negative = true;
        limit = Integer.MIN_VALUE;
        } else if (firstChar != '+') {
        throw NumberFormatException.forInputString(s, radix);
        }

        if (len == 1) { // Cannot have lone "+" or "-"
        throw NumberFormatException.forInputString(s, radix);
        }
        i++;
        }
        int multmin = limit / radix;
        int result = 0;
        while (i < len) {
        // Accumulating negatively avoids surprises near MAX_VALUE
        int digit = Character.digit(s.charAt(i++), radix);
        if (digit < 0 || result < multmin) {
        throw NumberFormatException.forInputString(s, radix);
        }
        result *= radix;
        if (result < limit + digit) {
        throw NumberFormatException.forInputString(s, radix);
        }
        result -= digit;
        }
        return negative ? result : -result;
        } else {
        throw NumberFormatException.forInputString(s, radix);
        }
        }
 */
posted @ 2022-05-05 18:44  xuchaoxin1375  阅读(13)  评论(0)    收藏  举报  来源