qq_35254395 2021-12-31 23:07 采纳率: 100%
浏览 12
已结题

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 块
所以想看看有没有其他办法

我想要达到的结果

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

  • 写回答

1条回答 默认 最新

  • 关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 1月8日
  • 已采纳回答 12月31日
  • 创建了问题 12月31日

悬赏问题

  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'
  • ¥15 vue+element项目中多tag时,切换Tab时iframe套第三方html页面需要实现不刷新
  • ¥50 深度强化学习解决能源调度问题