Java 直接在数组中添加对象
我写了一份学校作业的作业。现在我尝试将其添加到一个数组中,以便可以添加多个框 我的主要Java 直接在数组中添加对象,java,arrays,arraylist,Java,Arrays,Arraylist,我写了一份学校作业的作业。现在我尝试将其添加到一个数组中,以便可以添加多个框 我的主要 String name; double userInputLength; double userInputWidth; double userInputHeight; // initialise a scanner to be able to read the user input Scanner reader = new Sc
String name;
double userInputLength;
double userInputWidth;
double userInputHeight;
// initialise a scanner to be able to read the user input
Scanner reader = new Scanner(System.in);
// ask the user for input
System.out.print ("Enter the name of your box: ");
// read this input
name = (reader.nextLine());
// ask the user for input
System.out.print ("Enter the length of your box: ");
// read this input
userInputLength = (reader.nextDouble());
// ask the user for input
System.out.print ("Enter the width of your box: ");
// read this input
userInputWidth = (reader.nextDouble());
// ask the user for input
System.out.print ("Enter the height of your box: ");
// read this input
userInputHeight = (reader.nextDouble());
Block blockOne = new Block(name, userInputLength, userInputWidth, userInputHeight);
System.out.println( blockOne.showBoxAsString());
我的目标
package model;
import java.util.ArrayList;
import java.util.List;
public class Block {
// name variable of the figure
private String name;
// dimension variable of the figure
private double blockWidth;
private double blockHeight;
private double blockLength;
public Block() {
}
// form a block
public Block(String N, double L, double W, double H){
this.name = N;
this.blockLength = L;
this.blockWidth = W;
this.blockHeight = H;
}
// set the name
public void setBlockName(String N){ this.name = N; }
// set the name
public String getBlockName(){ return this.name; }
//set length method
public void setLength(double L)
{
this.blockLength = L;
}
//get length method
public double getLenght(){
return this.blockLength;
}
//set width method
public void setWidth(double W)
{
this.blockLength = W;
}
//get width method
public double getWidth(){
return this.blockWidth;
}
//set height method
public void setHeight(double H)
{
this.blockLength = H;
}
//get height method
public double getHeight(){
return this.blockHeight;
}
// method to calculate the surface of the shape (in this situation its a box)
public double calcSurfaceBox(){
// the formula to calculate the surface of the box is length times the width
double surface = 2 * (this.blockHeight * this.blockWidth) +
2 * (this.blockLength * this.blockHeight) +
2 * (this.blockWidth * this.blockLength) ;
// return the calculated value of surface
return surface;
}
// method to calculate the volume of the shape (in this situation it's a box)
public double calcVolumeBox(){
// the formula to calculate the volume is length times width times height
double volume = this.blockLength * this.blockWidth * this.blockHeight;
// return the calculated value of volume
return volume;
}
// a method to print a string to show the user the size of the shape (in this case a box.)
public String showBoxAsString(){
return String.format( "The box has a name: " + getBlockName() + "\n" +
"The box has a Length of: " + getLenght() + "\n" +
"The box has a Width of: " + getWidth() + "\n" +
"The height of the box is: " + getHeight());
}
}
我搜索了多种解决方案,但我无法找到。有人能给我一些工具或想法吗
我的目标是使我的主代码尽可能干净。因此,如果有人知道如何简化我的主代码,那将是非常棒的 初始化块对象数组
Block blocks[]=新块[长度]代码>
要访问每个对象,请使用块[索引]
注意:此处的数组元素存储对象的引用变量
编辑:此处的索引表示块对象,可能是blockOne在新扫描仪()之后创建数组和循环;如果您需要更明确的代码,请务必让我知道您是否需要someArray[index]=someValue
,或者这是