Java StatsArray with Exceptions

问题遇到的现象和发生背景

java 读取输入的值然后录入数组int [ ]
当输入n 为负时 抛出("Negative value. Not allowed.")
问题是负数n仍然会被赋值给数组
部分题目如下:• fillArrayFromUser method.

//o Using a do..while loop, a try block and two catch blocks, do the following:
// Prompt the user for a value. Your prompt should look like the example output. It should
//indicate to the user which element they are inputting a value into.
// Using Scanner, read each value input by the user as a String.
// Convert the value to an integer using Integer.parseInt( ).
// Invoke the checkIfNegative method to see if the number is negative.
// With a catch statement, handle NumberFormatException (an int was not entered) if it is thrown.
// With another catch statement, handle IllegalArgumentException if it is thrown.
// Assign the good value to the next element in the array 


问题相关代码,请勿粘贴截图
public  void fillArrayFromUser(){
        int i =0;
        do{
            System.out.println("Enter value ["+i+"] : ");
            int n=0;
            String string =scanner.nextLine();
            
            
            try { 
                
                n = Integer.parseInt(string);

                checkIfNegative(n);//判断是否为负

            } catch (NumberFormatException e) {
                System.out.println("Invalid value. Enter an int.");
            } catch (IllegalArgumentException e){
                System.out.println("Negative value. Not allowed.");
            }
            
            
            
            
            stats[i] = n;
            i++;
        }while(i<10);
        System.out.println("");
    }



    public  void checkIfNegative(int n) {
        if (n<0){
            throw new IllegalArgumentException( );    
        }
    }

运行结果及报错内容

Enter value [0] :
-1
Negative value. Not allowed.
Enter value [1] :
-2
Negative value. Not allowed.
Enter value [2] :
-3
Negative value. Not allowed.
Enter value [3] :
-4
Negative value. Not allowed.
Enter value [4] :
-5
Negative value. Not allowed.
Enter value [5] :
-6
Negative value. Not allowed.
Enter value [6] :
-7
Negative value. Not allowed.
Enter value [7] :
-8
Negative value. Not allowed.
Enter value [8] :
-9
Negative value. Not allowed.
Enter value [9] :
-10
Negative value. Not allowed.

Current Array values

    Stats [0] =-1
    Stats [1] =-2
    Stats [2] =-3
    Stats [3] =-4
    Stats [4] =-5
    Stats [5] =-6
    Stats [6] =-7
    Stats [7] =-8
    Stats [8] =-9
    Stats [9] =-10

Sum : -55
Average : -5.5
Max value: -1
Min value: -10

Sorted Now
Current Array values


    Stats [0] =-10
    Stats [1] =-9
    Stats [2] =-8
    Stats [3] =-7
    Stats [4] =-6
    Stats [5] =-5
    Stats [6] =-4
    Stats [7] =-3
    Stats [8] =-2
    Stats [9] =-1

Goodbye

我的解答思路和尝试过的方法

可以用if 判断但是题目要求中o说面使用 do..while 循环、一个 try 块和两个 catch 块
所以想看看有没有其他办法

我想要达到的结果

输入负数 应该报错并让用户重新输入

stats[i] = n; 和 i++; 放在 try 中 checkIfNegative(n);后面即可
你题目的解答代码如下:

public  void fillArrayFromUser(){
        int i =0;
        do{
            System.out.println("Enter value ["+i+"] : ");
            int n=0;
            String string =scanner.nextLine();
            
            try { 
                n = Integer.parseInt(string);
                checkIfNegative(n);//判断是否为负
                //放这里
                stats[i] = n;  
                i++;
            } catch (NumberFormatException e) {
                System.out.println("Invalid value. Enter an int.");
            } catch (IllegalArgumentException e){
                System.out.println("Negative value. Not allowed.");
            }
            
            
        }while(i<10);
        System.out.println("");
    }
 
    public  void checkIfNegative(int n) {
        if (n<0){
            throw new IllegalArgumentException( );    
        }
    }

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img