所以我刚开始使用ArrayList
和awt.Point
。我想在这里完成的是输出一个X和Y坐标的数组,因为炮弹在飞行中。但是,当我运行程序时,我在数组中得到了一堆Point[x=0,y=0]
。
我认为部分问题可能在回归Point
。我在Point
和bowling.Move()
中返回bowling.getLocation()
。一个人可能压倒另一个人?我觉得我接近我的结果,但却输掉了如何到达那里。
import java.awt.Point;
import java.util.ArrayList;
import java.util.Scanner;
public class Cannonball {
public static void main(String[] args) {
//Part 1: Open Scanner
Scanner keyboard = new Scanner(System.in);
//Part 2: Create a new cannonball
Ball bowling = new Ball(0);
//Part 3: Ask user for initial angle and starting velocity
System.out.println("Alright, give us the angle at which to fire: ");
bowling.setAngle(keyboard.nextDouble());
System.out.println("And what is the initial velocity: ");
bowling.setVel(keyboard.nextDouble());
//Part 4: Return the points of the cannonball's flight
for(int i=0; i<bowling.shoot.size(); i++) System.out.println(bowling.shoot);
//Part x: Close input
keyboard.close();
}
}
class Ball{
private double xPos, yPos, deltaSec;
private double alpha, v;
private double yVel, xVel;
private static final double gravity = -9.81;
public Ball(double xPos){
this.xPos=xPos;
yPos=0;
}
public Point move(double deltaSec){
xPos += xVel*deltaSec;
yPos += yPos*deltaSec;
return new Point();
}
public void yVel(){
yVel=v*Math.sin(alpha)*(deltaSec*gravity);
}
public void xVel(){
xVel=v*Math.cos(alpha);
}
public Point getLocation(double xPos, double yPos){
return new Point();
}
public void setAngle(double aplha){
this.alpha=alpha;
}
public void setVel(double v){
this.v=v;
}
public ArrayList<Point> shoot = new ArrayList<Point>();
{
while(deltaSec<60){
move(deltaSec);
shoot.add(getLocation(xPos, yPos));
deltaSec++;
}
}
}
答案 0 :(得分:0)
如果要返回表示该时间戳的x和y坐标的Point
,则应将它们传递给该点。 Point()
将创建一个坐标为0/0的点。
您应该调用Point(x,y)
构造函数(使用整数屏幕坐标 - 用于表示像素!)或使用Point2D
resp。 Point2D.Double( x, y)
。
<强>更新强>
以下是我想用于getLocation
的示例实现:
//Point version
public Point getLocation(double xPos, double yPos){
return new Point((int)xPos, (int)yPos); //Point only takes int coordinates
}
//Point2D version
public Point2D getLocation(double xPos, double yPos){
return new Point2D.Double( xPos, yPos );
}
move
如果需要返回一个点,也应该这样做。
更新2:
您似乎已添加此代码:
public ArrayList<Point> shoot = new ArrayList<Point>();
//I add this line to highlight the difference: the line above is no method signature
{
while(deltaSec<60){
move(deltaSec);
shoot.add(getLocation(xPos, yPos));
deltaSec++;
}
}
请注意,这是不方法,而是初始化程序块,它将在对象创建时运行。我想这不是故意的。