首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迭代时跳过对象

问迭代时跳过对象
EN

Stack Overflow用户
提问于 2022-05-16 18:44:04
回答 2查看 69关注 0票数 1

我有两个数组的对象Arr1是数组与竞争对手,Arr2是数组的障碍,如墙壁和轨道。

我需要反复通过每一个竞争对手,通过障碍,跳跃和跑步。如果竞争对手能够完成整个赛道,它应该打印如下:

代码语言:javascript
复制
"John jumped over wall"
"John finished track"
"John jumped over wall"

当选手不能跳转或完成径赛时,应该说

代码语言:javascript
复制
"John failed to jump over wall" 

别再重复其他障碍了。

代码语言:javascript
复制
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());

        }
    }
}

有了这段代码,我得到了这样的信息:

代码语言:javascript
复制
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. 

我需要这样的:

代码语言:javascript
复制
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 (竞争对手)的课程:

代码语言:javascript
复制
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);
        }
    }
}

墙壁和轨道:

代码语言:javascript
复制
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;
    } 
}

我也尝试过这样做,因为我不能在迭代时从列表中删除元素:

代码语言:javascript
复制
 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((我猜这是因为它正在删除元素,循环仍然希望迭代它们)。

EN

回答 2

Stack Overflow用户

发布于 2022-05-16 18:53:55

您可以使用break操作符停止循环迭代。示例:

代码语言:javascript
复制
    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;
        }
    }
票数 2
EN

Stack Overflow用户

发布于 2022-05-16 21:08:33

为了详细说明我的评论,你能做的是这样的事情

代码语言:javascript
复制
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;
    }
}

然后

代码语言:javascript
复制
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方法,例如。

代码语言:javascript
复制
@Override
public boolean passesJump(double height) {
    return jumpHeight > height;
}

在其他地方印刷。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72264129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档