gpt4 book ai didi

java - 从 ArrayList 获取列

转载 作者:行者123 更新时间:2023-12-01 09:18:38 24 4
gpt4 key购买 nike

所以我正在编写一个基于控制台的Shape Creator,它可以根据用户输入创建形状。如果用户想要创建 5 个 Square,程序会创建一个 ArrayList 并从具有 shapeId、面积等的 Square 类中获取数据。

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class ShapeMaker {


public ShapeMaker(){


create();

}

public void create(){
//creates list of arrays within shapes.
ArrayList<Circle>circles=new ArrayList<Circle>();
ArrayList<Square>squares=new ArrayList<Square>();
ArrayList<Rectangle>rectangles=new ArrayList<Rectangle>();

//this loop is dangerously infinity but I had to open the program for choices.
while(true){
String[] choices={"Create Random Shapes","Circles","Squares","Rectangles",
"Print All Shapes","Print Rectangles","Print Squares",
"Print Circles","Exit"};
String optionMenu = (String)JOptionPane.showInputDialog(
null,
"Choose what you desire",
"Customized Dialog",
JOptionPane.PLAIN_MESSAGE,
null,
choices,
choices[0]);


if (optionMenu==choices[0]){
// String input4=(String)JOptionPane.showInputDialog(null,"How Many Shape ?","Question",
// JOptionPane.QUESTION_MESSAGE);
// int inputLength4=Integer.parseInt(input4);
// for (int i=0;i<inputLength4;i++){
//
// circles2[i]=new Circle();
// circles.add(circles2[i]);
// squares2[i]=new Square();
// squares.add(squares2[i]);
// rectangles2[i]=new Rectangle();
// rectangles.add(rectangles2[i]);
//
// }
// JOptionPane.showMessageDialog(null,inputLength4+" shapes have been created successfully.");
}
else if (optionMenu==choices[1]) {
String input=(String)JOptionPane.showInputDialog(null,"How Many Circle ?","Question",
JOptionPane.QUESTION_MESSAGE);
int inputLength=Integer.parseInt(input);
Circle[]circles2=new Circle[inputLength];
if (circles.isEmpty()==false){
int delete=JOptionPane.showConfirmDialog(null, "There are circles in the list. Do you want to delete them?",
"Existing Shapes",JOptionPane.YES_NO_OPTION);
if (delete==JOptionPane.YES_OPTION){
circles.remove(circles2);
}
else{
for (int i=0;i<inputLength;i++){
circles2[i]=new Circle();
circles.add(circles2[i]);
}
}

}
else{


for (int i=0;i<circles2.length;i++){
circles2[i]=new Circle();
circles.add(circles2[i]);
}
JOptionPane.showMessageDialog(null,inputLength+" circle have been created successfully.");
}

}
else if (optionMenu==choices[2]) {
String input3=(String)JOptionPane.showInputDialog(null,"How Many Squares ?","Question",
JOptionPane.QUESTION_MESSAGE);
int inputLength3=Integer.parseInt(input3);
Square[]squares2=new Square[inputLength3];
if (squares.isEmpty()==false){
int delete=JOptionPane.showConfirmDialog(null, "There are squares in the list. Do you want to delete them?",
"Existing Shapes",JOptionPane.YES_NO_OPTION);
if (delete==JOptionPane.YES_OPTION){
squares.remove(squares2);
}
else{
for (int i=0;i<inputLength3;i++){
squares2[i]=new Square();
squares.add(squares2[i]);
}
}

}
else{

for (int i=0;i<inputLength3;i++){
squares2[i]=new Square();
squares.add(squares2[i]);
}
}
JOptionPane.showMessageDialog(null,inputLength3+" square have been created successfully.");


}
else if (optionMenu==choices[3]) {
String input2=(String)JOptionPane.showInputDialog(null,"How Many Rectangles ?","Question",
JOptionPane.QUESTION_MESSAGE);
int inputLength2=Integer.parseInt(input2);
Rectangle[]rectangles2=new Rectangle[inputLength2];
if (rectangles.isEmpty()==false){
int delete=JOptionPane.showConfirmDialog(null, "There are rectangles in the list. Do you want to delete them?",
"Existing Shapes",JOptionPane.YES_NO_OPTION);
if (delete==JOptionPane.YES_OPTION){
rectangles.remove(rectangles2);
}
else{
for (int i=0;i<inputLength2;i++){
rectangles2[i]=new Rectangle();
rectangles.add(rectangles2[i]);
}
}

}
else{
for (int i=0;i<inputLength2;i++){
rectangles2[i]=new Rectangle();
rectangles.add(rectangles2[i]);

}
JOptionPane.showMessageDialog(null,inputLength2+" rectangle have been created successfully.");
}
}
else if (optionMenu==choices[4]) {
System.out.printf("%-5s%-13s%-10s%-13s%-10s%-15s%n", "ID","Shape",
"Area","Full Shape", " Distance", "Properties");
System.out.println();
for (int i=0;i<rectangles.size();i++){
System.out.println(rectangles.get(i));
}
for (int i=0;i<squares.size();i++){
System.out.println(squares.get(i));
}
for (int i=0;i<circles.size();i++){
System.out.println(circles.get(i));
}


}
else if (optionMenu==choices[5]) {
System.out.printf("%-5s%-13s%-10s%-13s%-10s%-15s%n", "ID","Shape",
"Area","Full Shape", " Distance", "Properties");
System.out.println();
for (int i=0;i<rectangles.size();i++){
System.out.println(rectangles.get(i));

}
System.out.print("There are "+rectangles.size()+" rectangle with total area of ");
}
else if (optionMenu==choices[6]) {
System.out.printf("%-5s%-13s%-10s%-13s%-10s%-15s%n", "ID","Shape",
"Area","Full Shape", " Distance", "Properties");
System.out.println();
for (int i=0;i<squares.size();i++){
System.out.println(squares.get(i));

}
System.out.print("There are "+squares.size()+" square with total area of ");

}
else if (optionMenu==choices[7]) {
System.out.printf("%-5s%-13s%-10s%-13s%-10s%-15s%n", "ID","Shape",
"Area","Full Shape", " Distance", "Properties");
System.out.println();
for (int i=0;i<circles.size();i++){
System.out.println(circles.get(i));

}
System.out.print("There are "+circles.size()+" circle with total area of ");
}
else if(optionMenu==choices[8]) {
System.exit(1);

}
}

}

public static void main(String [] args){
new ShapeMaker();
// System.out.println(shape);


}
}

问题是,我需要从面积行获取值并计算这些形状的总面积。 Output

那么,如何获取创建的 ArrayList 的所有区域?

最佳答案

尊敬的埃格里莫。

正如您所说,您有一个 Square 类,它具有所有属性:IDShapeAreaFull形状距离属性

当你这样做时:

System.out.println(squares.get(i));

如果你想访问Area属性,你必须这样做:

  1. 在 Square 类中创建一个方法 getArea()。此方法必须是公共(public)的,并将返回正方形的面积。
  2. 从 System.out.println 调用 getArea() 方法来打印正方形区域:

System.out.println(squares.get(i).getArea()); 将打印您的正方形面积。

编辑:

如果您不知道如何创建 getArea() 方法,请查看以下示例:

public class Square{
int area;
int id;
// other attributes

// constructor

public int getArea(){ // This method will return your square area
return this.area;
}
}

祝你有美好的一天!

关于java - 从 ArrayList 获取列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40329561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com