该作业要求用户输入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);
}
}
这里是Cylinder类
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; }
}
答案 0 :(得分:2)
您的Cylinder
数组只有3个元素。 Cylinder[] volume = new Cylinder[3];
您的for loop
正在尝试访问此数组中元素2之后的元素。那些元素不存在。
答案 1 :(得分: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++)
这将确保它只迭代数组中存在的索引,无论它有多大或多小。
答案 2 :(得分: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,并在卷中删除++ [counter ++]
答案 3 :(得分:1)
首先,您的数组已声明为3。但是,在for循环中,您至少可以访问数组中的6个元素。因此,您将数组的大小增加到至少6.您应该更改代码:
volume[counter++] = new Cylinder(radius, height);
要
volume[counter] = new Cylinder(radius, height);
答案 4 :(得分:-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?
}