Try-catch没有捕获错误的数字输入(也得到java.lang.arithmatic异常/zero)
这是一个构造函数,我有一个测试平均类。我的作业要求我输入一个测试分数数组列表,作为输入验证,它希望我使用try-catch语句捕获0以下和100以上的任何输入 下面的构造函数从我的main中引入了一个数组列表,没有任何错误。然而,它并没有捕捉到负面输入。我已经看了两个多小时这个代码了,我想不出来。我想一双新的眼睛可能会明白为什么它没有捕捉到错误的输入 我的整个计划: 类别:Try-catch没有捕获错误的数字输入(也得到java.lang.arithmatic异常/zero),java,arraylist,try-catch,java.lang.class,Java,Arraylist,Try Catch,Java.lang.class,这是一个构造函数,我有一个测试平均类。我的作业要求我输入一个测试分数数组列表,作为输入验证,它希望我使用try-catch语句捕获0以下和100以上的任何输入 下面的构造函数从我的main中引入了一个数组列表,没有任何错误。然而,它并没有捕捉到负面输入。我已经看了两个多小时这个代码了,我想不出来。我想一双新的眼睛可能会明白为什么它没有捕捉到错误的输入 我的整个计划: 类别: import javax.swing.*; import java.util.*; class try2 {
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;
}
}
主要内容:
使用此代码可以解决您的问题
public try2(ArrayList<Integer> 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
嗯。这是工作代码。我刚修改了你的代码。根据命名标准,您应该使用类似“Try1”“Try2”的类名。若你们不想接受负值,你们应该立即抛出异常。但根据你的密码,在这里 在try2班
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 ");
}
}
}
在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<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);
}
}
我只更新了我修改过的代码。您需要根据需要进行修改。因为这是InputMismatchException…@luiggi,好吧,我试过了,但仍然没有发现任何问题,我猜我的for语句不能正常工作,您的代码需要的不仅仅是try-catch语句……所有这些答案都是错误的。测试是arraylist,因此必须检查当前元素是否超出范围。使用test.geti<0,而不是检查test是否<0。另外,请使用test.size,而不是test.sizex。@kevinsa5,您可以使用right.get,这更有意义,但是它仍然无法捕获负输入或任何超过100的内容。您是否建议使用另一种方法来捕获负输入。此代码中有几个问题:test.sizex不编译,test<0不编译,test>100不编译。this.userInputx=testx无法编译…您确定海报的意思是test.size<0吗?这是不可能的,首先,把它放在一个循环中是没有意义的。我想他们的意思是测试。geti<0 | | test.geti>100。是的。我的错误。sorryOk看看我的整个代码,出于某种原因,它没有捕捉到负数,而是抛出了一个算术错误。它说它除以零?我将数组列表与大小相加,如何除以0?@Min Naing Oo嘿,谢谢你的帮助我完成了我的程序,我接受了你的建议,这真的带来了很大的不同,我将在今天晚些时候发布我的全部代码,这样你就可以看到:@Erick你不应该更新你问题中更正的代码。如果我的建议解决了您的问题,请不要忘记通过单击勾选按钮接受问题。
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);
}
}