简单java汽车游戏中的for循环
这是一项任务,所以大部分结构都是规定的。我们有一个包含两类的套餐,一类是简单java汽车游戏中的for循环,java,for-loop,Java,For Loop,这是一项任务,所以大部分结构都是规定的。我们有一个包含两类的套餐,一类是Car,另一类是City。在城市的main方法中,cars已设置为在9,0时相互碰撞。我们必须编写代码,以便程序能够在汽车碰撞时进行检测。我正在查看City中的嵌套for循环,但不太清楚如何使其工作 车辆等级: public class Car { private int x ,y; private int facing; /* * where 0 = north, 1 = east, 2
Car
,另一类是City
。在城市的main
方法中,cars
已设置为在9,0时相互碰撞。我们必须编写代码,以便程序能够在汽车碰撞时进行检测。我正在查看City中的嵌套for循环,但不太清楚如何使其工作
车辆等级:
public class Car {
private int x ,y;
private int facing;
/*
* where 0 = north, 1 = east, 2 = south, 3 = west.
*/
private int distance;
public Car(int x, int y, int facing){
//"this" only has to be used because
//the variable names are identical
//"this" indicates that the variable
//declared at the top is being referenced.
//the x on the right-hand side is the local
//variable in the method
this.x = x;
this.y = y;
this.facing = facing;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getFacing() {
return facing;
}
public void setFacing(int facing) {
this.facing = facing;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
public void turnLeft() {
if (facing == 0) {
facing = 3;
}
else {
facing -= 1;
}
}
public void turnRight() {
if (facing == 3) {
facing = 0;
}
else {
facing += 1;
}
}
public void move (int distance) {
if (facing == 0) {
distance = distance + y;
y = distance;
}
else if (facing == 1) {
distance = distance + x;
x = distance;
}
else if (facing == 2) {
distance = y - distance;
y = distance;
}
else {
distance = x - distance;
x = distance;
}
}
}
城市等级:
import java.util.*;
public class City {
private List<Car> cars = new ArrayList<Car>();
private int sizeX;
private int sizeY;
public City (int sizeX, int sizeY) {
}
public void addCar(Car car) {
cars.add(car);
}
public void moveCar(Car car, int distance) {
for (int n = 0; n<distance; n++) {
car.move(1);
for (int i = 0; i<cars.size(); i++) {
Car otherCar = cars.get(i);
if (car != otherCar) {
////this is where I'm getting stuck
//trying to figure out how to use this
//to detect when cars run into each other
}
}
}
System.out.print("Car is at "+ "" + car.getX() + ","+ car.getY() +"\n");
}
public int getSizeX() {
return sizeX;
}
public void setSizeX(int sizeX) {
this.sizeX = sizeX;
}
public int getSizeY() {
return sizeY;
}
public void setSizeY(int sizeY) {
this.sizeY = sizeY;
}
public static void main(String[] args) {
City city = new City(10,10);
Car c1 = new Car(0,0,1);
Car c2 = new Car(9,9,2);
city.addCar(c1);
city.addCar(c2);
city.moveCar(c1,9);
city.moveCar(c2,9);
}
}
import java.util.*;
公营城市{
私家车列表=新的ArrayList();
私家侦探;
私营企业;
公共城市(国际大城市、国际大城市){
}
公共车辆{
cars.add(car);
}
公共车辆(小汽车,国际距离){
对于(int n=0;n,本质上,这是您的问题:
if (car != otherCar)
原因是:car
和otherCar
都是car
类型的对象,因此=
只会检查它们是否是完全相同的实例
常规做法是在Car
中覆盖equals()
。当两辆车位于同一坐标位置时,您似乎试图定义相等,因此我将在此处提供该提示
如果您的一个参数为null
或不是Car
类型,或者是同一个实例(您将使用=
),我将留给读者作为练习
@Override
public boolean equals(Object other) {
Car otherCar = (Car) other;
return otherCar.getX() == this.x && otherCar.getY() == this.y;
}
这就像作业的整个部分一样!首先自己尝试一下搜索如何比较javaCars中的对象,当它们的位置(X和Y)相等时,它们会相互碰撞你也不是在说你在哪个轴上移动car@hiafzhan,谢谢你的提示。我会对此做一些研究。检查其他
是否为空…@haifzhan:我说这是给读者的练习。我非常清楚这个等于
方法缺少什么;我只是不希望一个方法能够全权委托他将其复制到他们的代码中。就像全权委托!当您重写equals()
时,您应该根据需要调整hashCode()well@MaxZoom当前位置请参阅我先前的评论-这是读者的练习。