家庭作业 Java 数组



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

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

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

这是气缸测试(主要)

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 = "RadiustHeightn";
        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; }
}

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

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

数组

的大小为 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++)

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

您正在声明一个 3 大小的圆柱体数组,并且您正在尝试读取其中的 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,并且 value [counter++] 中删除了 ++

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

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

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

你确定吗?

    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?
    }

最新更新