我有一个程序,需要用户输入的长度和宽度,并将其添加到一个形状数组,并创建和添加每个形状后,它打印出来后,每一个。但是,如果其中一个值是负的(无效),那么它是不应该把那个形状到数组(这也是不应该印到每个步骤之后更新阵列)创建参数停止添加到数组?
这里是我的输出:
输入1:添加形状
输入2:删除的形状
输入9:退出
什么类型的形状?
矩形,三角形或圆形?
矩形
输入一个长度后面是高度
4.0 3.0
矩形长度:4.0高度:3.0面积:12.0
输入1:添加形状
输入2:删除形状
En之三9:退出
什么形状的类型?
矩形,三角形或圆形?
矩形
输入一个长度后面是高度
-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());
}
}
}
下面是它具有存取器和构建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");
}
}
}
我们*绝对*需要看到的前端,因为这是信息的如何获取到开始与对象。感谢您提供它。 – Makoto
实际上,你应该削减这个;只包含对此非常重要的代码。其他任何事情都是额外/过量的绒毛。 – Makoto
我对@Makoto道歉我在这里只是试图找出我的问题,并不确定如何优化,因为我是一个初学Java的人 –