嘿,我有一個點類,它會生成一個點列表。該類如下沒有引發異常消息
public class Point {
private double x, y;
public Point(double x, double y){
this.x = x;
this.y = y;
}//end constructor
public Point() {
// TODO Auto-generated constructor stub
}
public double getX(){
return x;
}
public void setX(double x) {
try{
if (x < 0){
throw new NegArgumentException();
}//end if
else{
this.x = x;
}//end else
}//end try
catch (NegArgumentException e){
System.out.println("error");
}//end catch
}
public double getY() {
return y;
}
public void setY(double y) {
try{
if (y < 0){
throw new NegArgumentException();
}//end if
else{
this.y = y;
}//end else
}//end try
catch (NegArgumentException e){
System.out.println("error");
}//end catch
}
public String toString(){
return "p(" + x + "," + y + ")";
}
}
我也創建了自己的異常類NegArgumentException異常如果生成負面點會引發異常。
public class NegArgumentException extends Exception {
public NegArgumentException(){}
public NegArgumentException(String message){
super(message);
}
}
我也有其他的2類矩形和圓形
public class Rectangle {
private Point upperLeft = new Point();
private Point lowerRight = new Point();
public Rectangle(Point upperLeft, Point lowerRight){
this.upperLeft = upperLeft;
this.lowerRight = lowerRight;
}//end constructor
public Point getUpperLeft() {
return upperLeft;
}
public void setUpperLeft(Point upperLeft) {
this.upperLeft = upperLeft;
}
public Point getLowerRight() {
return lowerRight;
}
public void setLowerRight(Point lowerRight) {
this.lowerRight = lowerRight;
}
public Point getUpperRight(){
return new Point(getLowerRight().getX(), getUpperLeft().getY());
}
public Point getLowerLeft(){
return new Point(getUpperLeft().getX(), getLowerRight().getY());
}
public double getArea(){
return upperLeft.getX()*lowerRight.getY();
}
public String toString(){
return "r[("+upperLeft.getX() + ","+upperLeft.getY()+"),("+lowerRight.getX()+","+lowerRight.getY()+")]";
}
}
和圓類
public class Circle {
private Point center = new Point();
private double radius;
public Circle(){}
public Circle(Point center, double radius){
this.center = center;
this.radius = radius;
}
public Point getCenter() {
return center;
}
public void setCenter(Point center) {
this.center = center;
}
public String toString(){
return "c[("+center.getX()+","+center.getY()+"), "+ radius+"]";
}
}
我的主類生成每個點的隨機列表這些對象。
import java.util.ArrayList;
import java.util.Random;
公共類主要{
public static void main(String[] args) {
//create arraylist for each class
ArrayList<Point> list = new ArrayList<Point>();
ArrayList<Rectangle> list1 = new ArrayList<Rectangle>();
ArrayList<Circle> list2 = new ArrayList<Circle>();
//create random generators
Random Point = new Random();
Random myGenerator = new Random();
int i = 0;
//go through for loop
while (i < 100){
int whichArray = myGenerator.nextInt(4) + 1;
//print out points for whichever random object
if (whichArray == 1){
int pointX = Point.nextInt(41) - 20;
int pointY = Point.nextInt(41) - 20;
if (pointX >= 0 && pointY >= 0){
list.add(new Point(pointX, pointY));
System.out.println(list.toString());
list.remove(0);
i++;
}
}//end if
if (whichArray == 2){
int pointX = Point.nextInt(41) - 20;
int pointY = Point.nextInt(41) - 20;
int pointRandom = Point.nextInt(20) - 20;
if (pointX >= 0 && pointY >= 0 && pointRandom >= 0){
list1.add(new Rectangle(new Point(pointX, pointY), new Point(pointX+pointRandom, pointY-pointRandom)));
System.out.println(list1.toString());
list1.remove(0);
i++;
}
}//end if
if (whichArray == 3){
int pointX = Point.nextInt(41) - 20;
int pointY = Point.nextInt(41) - 20;
int radius = Point.nextInt(41) - 20;
if (pointX >= 0 && pointY >= 0){
list2.add(new Circle(new Point(pointX, pointY),radius));
System.out.println(list2.toString());
list2.remove(0);
i++;
}
}//end if
}//end while loop
}//end main
}//end class
的問題是,我沒有看到在發生異常時顯示的任何消息。我知道這是工作,因爲我沒有得到我的觀點的負面座標。有誰知道爲什麼沒有消息顯示?
何時和爲什麼會發生異常?我看到'如果(pointX> = 0 && pointY> = 0)'守衛到處! – 2014-09-30 22:11:45
好的事情是,當我擺脫這些,任何數字,這將是消極的去0,所以我的隨機座標有更多的0比它應該@ElliottFrisch – Heyya 2014-09-30 22:13:39
爲什麼不只是你想開始的範圍? – 2014-09-30 22:15:10