获取我在Arraylist中构建的两种形状的区域以加在一起

时间:2014-07-11 18:51:48

标签: java arraylist

我正在研究一些代码,用户可以通过输入形状的边数,与这些边相关的数字(即长度和宽度,底边和高度)来构建矩形或三角形,并计算它们各自的区域并将其存储在Arraylist中。 所以我遇到的问题是我很难理解如何将形状的两个区域添加到TotalArea中。我正在使用的代码打印一些奇怪的数字,而不是两个区域的总和。代码使用继承并从suberClass和另外两个SubClasses调用,所以为简单起见,我不发布那些。 这是有问题的代码。

//Test Class

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;


   public class testPuzzle {

    public static boolean loop = false;


    public static void main(String[] args) throws IOException {


        ArrayList<Polygon> puzzle = new ArrayList<Polygon>();

        int userInput = 0;

        //Main Menu. Use do/while loop
        do {
            System.out.println("\nJigsaw Puzzle \nMenu:");
            System.out.println("1 - Add Polygon");
            System.out.println("2 - Print Polygon List");
            System.out.println("3 - Puzzle Area");
            System.out.println("4 - Exit");

            //Import Scanner
            Scanner keyStroke = new Scanner(System.in);

            System.out.print("\nEnter Menu Option: ");
            userInput = keyStroke.nextInt();

            //If statements to build the polygon
            if (userInput == 1){
                System.out.print("Enter Polygon Shape: r for rectangle, t for triangle. ");
                String shape = keyStroke.next();
                if (shape.equalsIgnoreCase("r")){
                    System.out.print("Name of Polygon: ");
                    String nameRectangle = keyStroke.next();
                    System.out.print("Number of Sides: ");
                    int numSidesRectangle = keyStroke.nextInt();
                    System.out.print("Rectangle Length: ");
                    double lengthRectangle = keyStroke.nextDouble();
                    System.out.print("Rectange Width: ");
                    double widthRectangle = keyStroke.nextDouble();
                    Polygon rectangle = new Rectangle(nameRectangle, numSidesRectangle, lengthRectangle, widthRectangle);
                     puzzle.add(rectangle);
                     System.out.println("Polygon added!");
                 }
                 if (shape.equalsIgnoreCase("t")){
                    System.out.print("Name of Polygon: ");
                    String nameTriangle = keyStroke.next();
                    System.out.print("Number of Sides: ");
                    int numSidesTriangle = keyStroke.nextInt();
                    System.out.print("Triangle Base: ");
                    double lengthTriangle = keyStroke.nextDouble();
                    System.out.print("Triangle Height: ");
                    double widthTriangle = keyStroke.nextDouble();
                    Polygon triangle = new Triangle(nameTriangle, numSidesTriangle, lengthTriangle, widthTriangle);
                    puzzle.add(triangle);
                    System.out.println("Polygon added!");

                } loop =true;
            }

            //calculate polygon areas and print polygon list
            if (userInput == 2){

                System.out.println("Polygon List (Name, Num Sides, Area):");
                for(int index = 0; index<puzzle.size(); index++){
                    Polygon list = puzzle.get(index);
                    System.out.println(list.getName()+", "+ list.getNumSides()+", "+ list.calculateArea());
                }
                loop =true;
            }
            //Print total polygon puzzle area
            if (userInput == 3){
                for(int index = 0; index<puzzle.size(); index++){
                    Polygon list = puzzle.get(index);
                    System.out.println("Total Puzzle Area: "+ list.calculateArea() +     list.calculatArea());

        }
            if  (userInput == 4){
                System.out.println("Goodbye!");
                System.exit(0);
        }
    }

       while (loop==true);
}

}

1 个答案:

答案 0 :(得分:0)

看起来你每次通过循环都会将每个区域添加到自身(即加倍)。据推测,这不是你想要做的事情,而是你应该在列表中总结所有的多边形。