2

该作业要求用户输入 3 个半径和 3 个高度条目,我将它们收集在一个数组中,然后确定每个条目的体积。我被困在阵列上。出于某种原因,我得到一个ArrayIndexOutOfBoundsException.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
(CylinderTest.java:19)

我在最后(第 6 个,或第三个条目的高度)收到错误。我不明白我做错了什么。我很难理解逻辑,这是我最大的问题。

这是 CylinderTest (主要)

import javax.swing.*;

//Driver class
public class CylinderTest
{

    public static void main(String[] args)
    {

        Cylinder[] volume = new Cylinder[3];

        for (int counter = 0; counter < 6; counter++)
        {
            double radius = Double.parseDouble(JOptionPane
                    .showInputDialog("Enter the radius"));
            double height = Double.parseDouble(JOptionPane
                    .showInputDialog("Enter the height"));
            volume[counter++] = new Cylinder(radius, height);
        }

        String display = "Radius\tHeight\n";
        for (Cylinder i : volume)
        {
            if (i != null)
                display += i.toString() + "\n";
        }
        JOptionPane.showMessageDialog(null, display);
    }
}

这是气缸类

public class Cylinder
{
    // variables
    public static final double PI = 3.14159;
    private double radius, height, volume;

    // constructor
    public Cylinder(double radius, double height)
    {
        this.radius = radius;
        this.height = height;
    }

    // default constructor
    public Cylinder()
    {this(0, 0);}

    // accessors and mutators (getters and setters)
    public double getRadius()
    {return radius;}

    public void setRadius(double radius)
    {this.radius = radius;}

    public double getHeight()
    {return height;}

    public void setHeight(double height)
    {this.height = height;}

    public double getVolume()
    {return volume;}

    public void setVolume(double volume)
    {this.volume = volume;}

    // Volume method to compute the volume of the cylinder
    public double volume()
    {return PI * radius * radius * height;}

    public String toString()
    {return volume + "\t" + radius + "\t" + height; }

}
4

5 回答 5

2

您的Cylinder数组只有 3 个元素。 Cylinder[] volume = new Cylinder[3];

for loop正在尝试访问此数组中元素 2 之后的元素。这些元素不存在。

于 2013-09-27T21:45:46.157 回答
1

您的数组大小为 3,由以下行确定:

Cylinder[] volume = new Cylinder[3];

然后在这里从 0 循环到 5:

for (int counter = 0; counter < 6; counter++)

然后您尝试在此处访问这些索引之一:

volume[counter++] = new Cylinder(radius, height);

由于数组的长度为 3,它只有索引 0、1 和 2。但您尝试访问高于 2 的索引。

作为旁注,我建议您将语句更改为,volume[counter] = new Cylinder(radius, height);否则您将在每次迭代中将 for 循环变量增加counter两次。

循环遍历数组中的索引时的一个好习惯是在条件中使用数组的长度:

for (int counter = 0; counter < volume.length; counter++)

这将确保它只遍历数组中存在的索引,无论它有多大或多小。

于 2013-09-27T21:45:35.317 回答
1

您正在声明一个 3 大小的 Cylinder 数组,并且您正在尝试读取其中的 6 个。

Cylinder[] volume = new Cylinder[3]; // 3 size array

        for (int counter = 0; counter < 6; counter++) // loop 6 times
        {
            double radius = Double.parseDouble(JOptionPane
                    .showInputDialog("Enter the radius"));
            double height = Double.parseDouble(JOptionPane
                    .showInputDialog("Enter the height"));
            volume[counter++] = new Cylinder(radius, height); // read 0, 1, 2, 3, 4, 5...
        }

那应该是:

Cylinder[] volume = new Cylinder[3]; // 3 size array

        for (int counter = 0; counter < volume.length; counter++) // loop 6 times
        {
            double radius = Double.parseDouble(JOptionPane
                    .showInputDialog("Enter the radius"));
            double height = Double.parseDouble(JOptionPane
                    .showInputDialog("Enter the height"));
            volume[counter] = new Cylinder(radius, height);
        }

注意 volume.length 而不是 6,并且在 volume[counter++] 中删除了 ++

于 2013-09-27T21:45:43.550 回答
1

首先,您的数组已被声明为大小为 3。但是,在您的 for 循环中,您至少可以访问数组的 6 个元素。因此,您将数组的大小至少增加到 6。您应该更改代码:

volume[counter++] = new Cylinder(radius, height);

volume[counter] = new Cylinder(radius, height);
于 2013-09-27T21:45:44.287 回答
-1

你确定吗?

    Cylinder[] volume = new Cylinder[3]; 

    for (int counter = 0; counter < 6; counter++)
    {
        double radius = Double.parseDouble(JOptionPane
                .showInputDialog("Enter the radius"));
        double height = Double.parseDouble(JOptionPane
                .showInputDialog("Enter the height"));
        volume[counter++] = new Cylinder(radius, height); 
        //counter will count up to 5 (Array out of Bounds exception for sure..)
        //also: why are you incrementing counter by yourself?
    }
于 2013-09-27T21:51:24.730 回答