创建停止添加到数组的参数?

时间:2016-11-09 21:22:06

标签: java

我有一个程序,它接受用户输入的长度和宽度,并将其添加到形状数组中,在创建和添加每个形状后,它会在每个形状之后打印出来。但是,如果其中一个值为负(无效),那么它不应该将该形状放入数组中(在每个步骤之后也不应该将其打印到更新的数组中)

这是我的输出:

  

输入1:添加形状

     

输入2:删除形状

     

输入9:退出

     

1

     

什么类型的形状?

     

矩形,三角形还是圆形?

     

矩形

     

输入长度,后跟高度

     

4.0 3.0

     

矩形长度:4.0高度:3.0面积:12.0

     

输入1:添加形状

     

输入2:删除形状

     

输入9:退出

     

1

     

什么类型的形状?

     

矩形,三角形还是圆形?

     

矩形

     

输入长度,后跟高度

     

-3.0 5.0

     

长度无效

     

矩形长度:0.0高度:5.0面积:0.0

     

矩形长度:4.0高度:3.0面积:12.0

     

输入1:添加形状

     

输入2:删除形状

     

输入9:退出

那个粗体部分不应该存在,这是我的问题

这里是包含我的方法的ShapeCollection:

    private Shape[] shapes;
private int index;

public ShapeCollection() {
    this.shapes = new Shape[10];
    index = 0;
}

public ShapeCollection(int size) {
    this.shapes = new Shape[size];
    index = 0;
}

//Accessors
public Shape[] getShapes() {
    return shapes;
}

//Mutators
public void setShapes(Shape[] shapes) {
    this.shapes = shapes;
}

//Methods
public void addShape(Shape shape) {
    this.sortShapes();
    if (index > shapes.length - 1) {
        System.out.println("The shape collector is full");
    } else {
        shapes[index] = shape;
        index++;
    }
}

private void sortShapes() {
    for (int i = 0; i < this.shapes.length; i++) {
        int smallestIndex = i;
        for (int j = i; j < this.shapes.length; j++) {
            if (shapes[j] != null && shapes[smallestIndex] != null) {
                if (shapes[j].getArea() < shapes[smallestIndex].getArea()) {
                    smallestIndex = j;
                }
            }
        }
        if (smallestIndex != i) {
            Shape temp = shapes[i];
            shapes[i] = shapes[smallestIndex];
            shapes[smallestIndex] = temp;
        }
    }

}

public void removeShape(String type, double area) {
    this.sortShapes();
    for (int i = 0; i < shapes.length; i++) {
        if (shapes[i].getShapeType().equalsIgnoreCase(type) && shapes[i].getArea() == area) {
            shapes[i] = null;
            index--;
            break;
        }
    }
    for (int i = 0; i < shapes.length - 1; i++) {
        if (shapes[i] == null && shapes[i + 1] != null) {
            for (int j = i; j < shapes.length - 1; j++) {
                shapes[j] = shapes[j + 1];
            }
            break;
        }
    }
}

public void printShapes() {
    this.sortShapes();
    for (int i = 0; i < shapes.length; i++) {
        if (shapes[i] != null) {
            System.out.println(shapes[i].getShapeType() + " " + shapes[i].toString() + " Area: " + shapes[i].getArea());
        }
    }
}

这里是具有mutators和构造的Rectangle类:

private double length;
private double width;

//Default construct
public Rectangle() {
    this.length = 0.0;
    this.width = 0.0;
}

//Parameterized construct
public Rectangle(double aLength, double aWidth) {
    this.setLength(aLength);
    this.setWidth(aWidth);
}

//Accessors
public double getLength() {
    return this.length;
}

public double getWidth() {
    return this.width;
}

//Mutators
public void setLength(double newLength) {
    if (newLength < 0) {
        System.out.println("Invalid length");
        return;
    }
    this.length = newLength;
}

public void setWidth(double newWidth) {
    if (newWidth < 0) {
        System.out.println("Invalid width");
        return;
    }
    this.width = newWidth;
}

public String getShapeType() {
    return "Rectangle";
}

public double getArea() {
    return (this.getLength() * this.getWidth());
}

public String toString() {
    return "Length: " + this.getLength() + " Height: " + this.getWidth();
}

这是您可能不需要看到的前端:

    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    boolean run = true;
    ShapeCollection shapes = new ShapeCollection();

    System.out.println("Welcome to the Shapes collections");

    while (run == true) {

        System.out.println("Enter 1: Add a shape\nEnter 2: Remove a shape\n" +
                "Enter 9: Quit");

        int input = keyboard.nextInt();

        if (input == 1) {
            System.out.println("What type of shape?\nRectangle, Triangle, or Circle?");
            String type = keyboard.next();

            if (type.equalsIgnoreCase("Rectangle")) {
                System.out.println("Enter a length followed by a height");
                double length = keyboard.nextDouble();
                double height = keyboard.nextDouble();
                shapes.addShape(new Rectangle(length, height));
                System.out.println();
                shapes.printShapes();

            } else if (type.equalsIgnoreCase("Triangle")) {
                System.out.println("Enter a base followed by a height");
                double base = keyboard.nextDouble();
                double height = keyboard.nextDouble();
                shapes.addShape(new Triangle(base, height));
                System.out.println();
                shapes.printShapes();

            } else if (type.equalsIgnoreCase("Circle")) {
                System.out.println("Enter a radius");
                double radius = keyboard.nextDouble();
                shapes.addShape(new Circle(radius));
                System.out.println();
                shapes.printShapes();
            } else if (input == 2) {
                System.out.println("Enter the shape type");
                type = keyboard.next();
                System.out.println("Enter an area");
                double area = keyboard.nextDouble();
                shapes.removeShape(type, area);
                System.out.println();
                shapes.printShapes();
                System.out.println();
            } else if (input == 9) {
                run = false;
                System.out.println("Goodbye");
            }
        } else {
            System.out.println("Invalid input");
        }
    }

}

3 个答案:

答案 0 :(得分:1)

问题是,您创建了一个新形状,然后检查输入是否有效。我建议你检查前端的有效性。

您可以创建一个方法来检查:

Preds

答案 1 :(得分:0)

问题是您正在验证Rectangle构造函数中的输入。您应该在将输入传递给构造函数之前验证输入(并且在失败时不创建对象)。

但是你可以通过抛出异常来修复这个破碎的设计,而不仅仅是输出检测到的不一致。

/* BTW: never call public methods from a constructor !!! */
private void setWidth(double newWidth) {
    if (newWidth < 0) {
        throw new IllegalArgumentException(newWidth +" is less than 0.0!" );
    }
    this.width = newWidth;
}

因此,您必须在try/catch级联中添加if/else块...

答案 2 :(得分:-1)

为什么你把错误的形状放入阵列? 这样想:
您将输入数据输入程序
您必须验证数据,是否正确 如果是,请将其放入阵列中 如果没有显示错误消息,请不要将其放入数组中  那就是它。

您将在何处验证您的数据取决于您  您可以在调用shapes.addShape之前在前端执行此操作  或者在shapes.addShape内部,如果输入数据错误,则不增加索引。