1

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

2 回答 2

0

好的。这是工作代码。我刚刚修改了你的代码。根据命名标准,您应该使用类名,如“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);

        }
    }

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

于 2014-02-19T02:47:28.437 回答
-2

use this code it will solve your problem

 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
于 2014-02-19T02:43:44.937 回答