尝试捕捉没有赶上一个糟糕的数字输入(也可以得到一个java.lang.arithmatic例外/零) [英] Try catch is not catching a bad number input ( Also getting a java.lang.arithmatic exception / zero)

查看:165
本文介绍了尝试捕捉没有赶上一个糟糕的数字输入(也可以得到一个java.lang.arithmatic例外/零)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个构造函数,我有一个测试平均班级。我的作业要求我考试分数的数组列表,并输入验证带来,它要我使用try catch语句赶在0和100的任何输入。

构造下面带来的是在一个数组列表从我的主要有任何错误。然而,它不醒目的负输入端。我一直在研究这个code了两个多小时,我无法弄清楚。我想到了一个新鲜的一双眼睛大概可以明白为什么它没有赶上不好的输入。

我的整个程序:

类:

 进口的javax.swing *。
进口的java.util。*;
类try2
{
    公共静态的ArrayList<整数GT; userInput =新的ArrayList<整数GT;();
    公共静态双平均值;公共try2()
{
}公共try2(ArrayList的<整数GT;测试)
{
  为(中间体X = 0; X&下; = test.size(); X ++)
  {
      尝试
      {
        如果(test.get(x)的℃,|| test.get(x)的大于100)
        {
            抛出新抛出:IllegalArgumentException();
        }
        其他
        {
            this.userInput =测试;
        }
     }
    赶上(抛出:IllegalArgumentException前){
    JOptionPane.showMessageDialog(NULL,NO NEGETIVES ALLOWED);
    }
  }
}公共静态无效setAvg()
{
        INT总和= 0;
        为(中间体X = 0; X&下; userInput.size(); X ++)
        {
            总和+ = userInput.get(X);
        }
        平均=总和/ userInput.size();
}公共静态双getAvg()
{
    平均回报;
}}

主营:

 进口的javax.swing *。
进口的java.util。*;公共类try1
{公共静态的ArrayList<整数gt;用户=新的ArrayList<整数GT;();
私有静态try2测试=新try2(用户);
公共静态整数testnum;公共静态无效的主要(字串[] args)
{
    testnum =的Integer.parseInt(JOptionPane.showInputDialog(NULL,请输入测试量要低于计算));
类();
}
公共静态无效类()
{
    INT userInput = 0;
            如果(userInput == JOptionPane.YES_OPTION)
            {
                对于(诠释计数= 1;计数< = testnum;计数++)
                {
                    字符串userInputString = JOptionPane.showInputDialog(NULL,请填写所有下面的测试等级标准计算方法」);
                    int值=的Integer.parseInt(userInputString);
                    user.add(值);
                }            如果(userInput == JOptionPane.NO_OPTION)
            {
                testing.setAvg();
                JOptionPane.showMessageDialog(NULL,你平均+(testing.getAvg()));
            }
    }
}


解决方案

确定。这里正在code。我只是修改了code。根据命名标准,你应该使用类的名称,如Try1'Try2。你应该立即抛出异常,如果你不想接受负值。但是,根据您的code,在这儿呢。

在try2类,

 公共try2(ArrayList的<整数GT;测试){
        为(中间体X = 0; X&下; = test.size() - 1; X ++){
            尝试{
                如果(test.get(x)的℃,|| test.get(x)的大于100){
                    抛出新抛出:IllegalArgumentException();
                }其他{
                    this.userInput =测试;
                }
            }赶上(抛出:IllegalArgumentException前){
                JOptionPane.showMessageDialog(NULL,NO NEGETIVES ALLOWED);
            }
        }
    }

在try1类,

 公共静态无效的主要(字串[] args){
        testnum =
                的Integer.parseInt(JOptionPane.showInputDialog(NULL,
                        请输入测试量要低于计算));
        类();
    }    公共静态无效类(){
        INT userInput = 0;
        如果(userInput == JOptionPane.YES_OPTION){
            用户=新的ArrayList<整数GT;();
            对于(诠释计数= 1;计数< = testnum;计数++){
                字符串userInputString =
                        JOptionPane.showInputDialog(NULL,请填写所有下面的测试等级标准计算方法」);
                int值=的Integer.parseInt(userInputString);
                user.add(值);
            }            如果(userInput == JOptionPane.NO_OPTION){
                testing.setAvg();
                JOptionPane.showMessageDialog(NULL,你平均+(testing.getAvg()));
            }            新try2(用户);        }
    }

我只更新了codeS我修改。您需要修改,​​因为你需要。

This is a Constructor that I have for a test average class. My assignment asks me to bring in an array-list of test score and as input validation, it wants me to use a try catch statement to catch any input under 0 and over 100.

The constructor below is bringing in an array-list from my main with out any error. However, it is not catching a negative input. I been looking at this code for over two hours and I can't figure it out. I thought a fresh pair of eyes could probably see why it is not catching bad input.

My whole Program:

class:

import javax.swing.*;
import java.util.*;


class try2
{
    public static ArrayList<Integer>userInput=new ArrayList<Integer>();
    public static double avg;

public try2()
{
}

public try2(ArrayList<Integer> test) 
{
  for ( int x = 0 ; x <= test.size(); x++)
  {
      try
      { 
        if ( test.get(x) < 0 || test.get(x) > 100) 
        {
            throw new IllegalArgumentException ();
        } 
        else
        {
            this.userInput = test;  
        }
     } 
    catch ( IllegalArgumentException ex) {
    JOptionPane.showMessageDialog(null," NO NEGETIVES ALLOWED ");
    }
  }   
}

public static void setAvg ()
{
        int sum = 0;
        for ( int x = 0 ; x < userInput.size(); x++)
        {
            sum += userInput.get(x) ;
        }
        avg = sum / userInput.size();   
}

public static double getAvg ()
{
    return avg;
}

}

Main:

import javax.swing.*;
import java.util.*;

public class try1
{   

public static ArrayList<Integer>user=new ArrayList<Integer>();
private static try2 testing = new try2 (user);
public static Integer testnum;  

public static void main (String[] args)
{
    testnum = Integer.parseInt(JOptionPane.showInputDialog(null, "Please Enter The Amount Of Test To Be Calculated Below "));
classes ();
}


public static void classes ()
{
    int userInput = 0;
            if (userInput == JOptionPane.YES_OPTION)
            {
                for ( int count = 1; count <= testnum; count++)
                {   
                    String userInputString = JOptionPane.showInputDialog(null, " PLEASE ENTER ALL THE FOLLOWING TEST GRADES TO CALCULATE ");
                    int value = Integer.parseInt(userInputString);
                    user.add(value);
                }

            if (userInput == JOptionPane.NO_OPTION )
            {
                testing.setAvg ();
                JOptionPane.showMessageDialog(null,"You average is" + (testing.getAvg()));
            }
    }
}

解决方案

Ok. Here is working code. I just modified your code. According to Naming standard, you should use Class name like 'Try1' 'Try2'. And you should immediately throw exception if you don't wanna accept negative value. But according to your code, here it is.

In try2 class,

public try2(ArrayList<Integer> test) {
        for (int x = 0; x <= test.size()-1; x++) {
            try {
                if (test.get(x) < 0 || test.get(x) > 100) {
                    throw new IllegalArgumentException();
                } else {
                    this.userInput = test;
                }
            } catch (IllegalArgumentException ex) {
                JOptionPane.showMessageDialog(null, " NO NEGETIVES ALLOWED ");
            }
        }
    }

In try1 class,

public static void main(String[] args) {
        testnum =
                Integer.parseInt(JOptionPane.showInputDialog(null,
                        "Please Enter The Amount Of Test To Be Calculated Below "));
        classes();
    }

    public static void classes() {
        int userInput = 0;
        if (userInput == JOptionPane.YES_OPTION) {
            user = new ArrayList<Integer>();
            for (int count = 1; count <= testnum; count++) {
                String userInputString =
                        JOptionPane.showInputDialog(null, " PLEASE ENTER ALL THE FOLLOWING TEST GRADES TO CALCULATE ");
                int value = Integer.parseInt(userInputString);
                user.add(value);
            }

            if (userInput == JOptionPane.NO_OPTION) {
                testing.setAvg();
                JOptionPane.showMessageDialog(null, "You average is" + (testing.getAvg()));
            }

            new try2(user);

        }
    }

I only updated the codes I modified. You need to modify as you need.

这篇关于尝试捕捉没有赶上一个糟糕的数字输入(也可以得到一个java.lang.arithmatic例外/零)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆