尝试catch没有捕获错误的数字输入(也得到java.lang.arithmaticexception/零)

这是我用于测试平均类的构造函数。 我的任务要求我引入一个测试分数列表并作为输入validation,它希望我使用try catch语句来捕获0和100以上的任何输入。

下面的构造函数是从我的main引入一个数组列表,没有任何错误。 然而,它没有捕获负面的输入。 我一直在看这个代码超过两个小时,我无法弄明白。 我认为一双新鲜的眼睛可能会明白为什么它没有抓住糟糕的输入。

我的整个计划:

类:

import javax.swing.*; import java.util.*; class try2 { public static ArrayListuserInput=new ArrayList(); public static double avg; public try2() { } public try2(ArrayList test) { for ( int x = 0 ; x <= test.size(); x++) { try { if ( 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; } } 

主要:

 import javax.swing.*; import java.util.*; public class try1 { public static ArrayListuser=new ArrayList(); 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())); } } } 

好。 这是工作代码。 我刚刚修改了你的代码。 根据命名标准,您应该使用类名称’Try1”Try2’。 如果你不想接受负值,你应该立即抛出exception。 但根据你的代码,这里是。

在try2类中,

 public try2(ArrayList 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 "); } } } 

在try1类中,

 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(); 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); } } 

我只更新了我修改的代码。 您需要根据需要进行修改。

使用此代码它将解决您的问题

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