首先我是一个java初学者,所以如果我使用了错误的词汇,请原谅。
问题是我正在使用 2 个类,我似乎无法让我的构造函数保持点 x 和 y 坐标的值。我一直在尝试不同的方法,但似乎无法得到它。任何帮助,将不胜感激。
import java.awt.Point;
公共类 FindRoute {
private static boolean randomRoute = false;
/** Driver for the FindRoute project.
*
* @param args an array of four integers containing [x coordinate of car, y coordinate of car,
* x coordinate of destination, ycoordinate of destination]
*/
public static void main(String[] args)
{
if (args.length<5)
{
System.err.println( "Usage java FindRoute id Xstart Ystart Xend Yend [random]");
System.exit(1);
}
String carId = args[0];
int xCar = Integer.parseInt(args[1]);
int yCar = Integer.parseInt(args[2]);
int xDestination = Integer.parseInt(args[3]);
int yDestination = Integer.parseInt(args[4]);
Car car = new Car(new Point(xCar, yCar), carId);
System.out.println(car);
car.setDestination(new Point(xDestination, yDestination));
System.out.println(car);
System.out.println("xcar= " + xCar);
System.out.println("ydest = " + yDestination);
if (args.length == 6) {
if (args[5].startsWith("r"))
car.setRandomRoute(true);
}
System.out.println(car);
}
然后是构造函数和toString
public Car (Point car, String carID) {
this.xCar = xCar;
this.yCar = yCar;
this.carID= carID;
public String toString() {
return "Car [id = " + carID + ", location = [x=" + xCar + ", y=" + yCar + "], destination = [x=" + xDestination + ", y=" + yDestination + "]]";
我的输出将拉动字符串,但将汽车点设置为 0,0。如果这是不正确的提问方式,请给我提示。提前致谢