我有两个数组的对象Arr1是数组与竞争对手,Arr2是数组的障碍,如墙壁和轨道。
我需要反复通过每一个竞争对手,通过障碍,跳跃和跑步。如果竞争对手能够完成整个赛道,它应该打印如下:
"John jumped over wall"
"John finished track"
"John jumped over wall"当选手不能跳转或完成径赛时,应该说
"John failed to jump over wall" 别再重复其他障碍了。
Obstacle[] obstacles = {
new Track(random.nextInt(100, 3000)),
new Wall(random.nextDouble(0.5, 2.0)),
new Track(random.nextInt(100, 3000)),
new Wall(random.nextDouble(0.5, 2.0)),
};
List<Obstacle> obstacleList = new ArrayList<>(List.of(obstacles));
Competor[] competors = {
new Human("John", random.nextDouble(0.5, 2.0)*10/10, random.nextInt(100, 3000)),
new Cat("Snowball", random.nextDouble(0.5, 2.0)*10/10, random.nextInt(100, 3000)),
new Robot("Optimus", random.nextDouble(0.5, 2.0)*10/10, random.nextInt(100, 3000)),
};
List<Competor> competorList = new ArrayList<>(List.of(competors));
for (Competor c : competorList) {
for (Obstacle o : obstacleList) {
if (o instanceof Wall) {
c.jump(((Wall) o).getM());
}
if (o instanceof Track) {
c.run(((Track) o).getM());
}
}
}有了这段代码,我得到了这样的信息:
Human John have successfully jumped over '0,7'm. wall.
Human John have successfully finished '509'm. track.
Human John failed to jump over '1,9'm. wall.
Human John failed to finish '2439'm. track.
Cat Snowball failed to jump over '0,7'm. wall.
Cat Snowball have successfully finished '509'm. track.
Cat Snowball failed to jump over '1,9'm. wall.
Cat Snowball failed to finish '2439'm. track.
Robot Optimus have successfully jumped over '0,7'm. wall.
Robot Optimus have successfully finished '509'm. track.
Robot Optimus failed to jump over '1,9'm. wall.
Robot Optimus failed to finish '2439'm. track. 我需要这样的:
Human John have successfully jumped over '0,7'm. wall.
Human John have successfully finished '509'm. track.
Human John failed to jump over '1,9'm. wall.
Cat Snowball failed to jump over '0,7'm. wall.
Robot Optimus have successfully jumped over '0,7'm. wall.
Robot Optimus have successfully finished '509'm. track.
Robot Optimus failed to jump over '1,9'm. wall. 机器人,猫,人类只是基于母班Competor (竞争对手)的课程:
public class Human extends Competor implements Run, Jump {
String type;
String name;
double jumpHeight;
int runDistance;
public Human(String name, double jumpHeight, int runDistance) {
this.type = this.getClass().getSimpleName();
this.name = name;
this.jumpHeight = jumpHeight;
this.runDistance = runDistance;
}
public double getJumpHeight() {
return jumpHeight;
}
public int getRunDistance() {
return runDistance;
}
@Override
public void jump(double height) {
if (jumpHeight > height) {
System.out.printf("%s %s have successfully jumped over '%.1f'm. wall. \n", type, name, height);
} else {
System.out.printf("%s %s failed to jump over '%.1f'm. wall. \n", type, name, height);
}
}
@Override
public void run(int distance) {
if (runDistance > distance) {
System.out.printf("%s %s have successfully finished '%d'm. track. \n", type, name, distance);
} else {
System.out.printf("%s %s failed to finish '%d'm. track. \n", type, name, distance);
}
}
}墙壁和轨道:
public class Wall extends Obstacle {
double height;
public double getM() {
return height;
}
public Wall(double height) {
this.height = height;
}
}
public class Track extends Obstacle{
int distance;
public int getM() {
return distance;
}
public Track(int distance) {
this.distance = distance;
}
}我也尝试过这样做,因为我不能在迭代时从列表中删除元素:
ListIterator<Competor> competorListIter = competorList.listIterator();
while(competorListIter.hasNext()) {
for (Obstacle o : obstacleList){
if (o instanceof Wall) {
if (((Wall) o).getM() > competorListIter.next().getJumpHeight()) {
competorListIter.next().jump(((Wall) o).getM());
competorListIter.remove();
} else {
competorListIter.next().jump(((Wall) o).getM());
}
}
if (o instanceof Track) {
if (((Track) o).getM() > competorListIter.next().getRunDistance()) {
competorListIter.next().run(((Track) o).getM());
competorListIter.remove();
} else {
competorListIter.next().run(((Track) o).getM());
}
}
}
}但是这给了NoSuchElementException((我猜这是因为它正在删除元素,循环仍然希望迭代它们)。
发布于 2022-05-16 18:53:55
您可以使用break操作符停止循环迭代。示例:
int[] heightArr = new int[] {3, 1, 4, 5};
int carlJumpHeight = 3;
for (int height : heightArr) {
if (carlJumpHeight < height) {
System.out.println("Carl can't jump so high!");
break;
}
}发布于 2022-05-16 21:08:33
为了详细说明我的评论,你能做的是这样的事情
public boolean passesJump(double height) {
if (jumpHeight > height) {
System.out.printf("%s %s have successfully jumped over '%.1f'm. wall. \n", type, name, height);
return true;
} else {
System.out.printf("%s %s failed to jump over '%.1f'm. wall. \n", type, name, height);
return false;
}
}然后
for (Competor c : competorList) {
for (Obstacle o : obstacleList) {
if (o instanceof Wall && !c.passesJump(((Wall) o).getHeight());
break;
}
if (o instanceof Track && !c.passesRun(((Track) o).getDistance()) {
break;
}
}
}或者简化passesX方法,例如。
@Override
public boolean passesJump(double height) {
return jumpHeight > height;
}在其他地方印刷。
https://stackoverflow.com/questions/72264129
复制相似问题