2016-11-09 11 views
-2

我有一个程序,需要用户输入的长度和宽度,并将其添加到一个形状数组,并创建和添加每个形状后,它打印出来后,每一个。但是,如果其中一个值是负的(无效),那么它是不应该把那个形状到数组(这也是不应该印到每个步骤之后更新阵列)创建参数停止添加到数组?

这里是我的输出:

输入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"); 
     } 
    } 

} 
+0

我们*绝对*需要看到的前端,因为这是信息的如何获取到开始与对象。感谢您提供它。 – Makoto

+0

实际上,你应该削减这个;只包含对此非常重要的代码。其他任何事情都是额外/过量的绒毛。 – Makoto

+0

我对@Makoto道歉我在这里只是试图找出我的问题,并不确定如何优化,因为我是一个初学Java的人 –

回答

-1

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

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

+0

等等,什么?这甚至可以回答这个问题? – Makoto

+0

我比以前感到​​困惑 –

+0

阅读答案。 – Vadim

1

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

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

public boolean isValid(double value){ 
    if (value < 0.0) { 
     System.out.println("Invalid Input"); 
     return false; 
    }else {return true;} 
} 
+0

谢谢!但是,我需要为每个变量类型(长度,宽度,基准,高度,半径)创建其中的一个吗?我只想着想如何最好地实现它 –

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; 
} 

原因你就必须添加在你if/else级联的try/catch块...