Java 代码执行顺序的影响

Java 代码执行顺序的影响,java,methods,Java,Methods,我遇到的问题是,当我更改顺序时,代码停止工作 例如,在下面的代码中,我的对象不会显示,除非它们是第一个。除非我的机器人在上面,否则它不会出现。如果我将makeWalls移动到底部,墙就会消失 我的问题是,是否有任何方法可以构造我的代码,以便顺序不重要,并且所有东西都可以一起工作 import becker.robots.*; import java.util.Random; public class Assignment4 { public static void main(Strin

我遇到的问题是,当我更改顺序时,代码停止工作

例如,在下面的代码中,我的对象不会显示,除非它们是第一个。除非我的机器人在上面,否则它不会出现。如果我将
makeWalls
移动到底部,墙就会消失

我的问题是,是否有任何方法可以构造我的代码,以便顺序不重要,并且所有东西都可以一起工作

import becker.robots.*;
import java.util.Random;

public class Assignment4 {
    public static void main(String[] args) { 
        City newYork = new City();

        makeWalls(newYork);
        Robot hammer = new Robot(newYork, 1, 1, Direction.EAST);
        Thing picnic= new Thing(newYork, 2,3);
        Thing apple= new Thing(newYork, 2,4);
        Thing corndog= new Thing(newYork, 2,4);
        Thing cottoncandy= new Thing(newYork, 2,4);
        Thing routine= new Thing(newYork, 1,4);
        Thing man= new Thing(newYork, 3,4);
        Thing eating= new Thing(newYork, 5,4);
        Thing cookies= new Thing(newYork, 4,4);
        makeRobotReach(hammer);
    }

    // Robot method
    public static void makeRobotReach(Robot r){
        while(r.frontIsClear()){
            r.move();
        }
    }

    public static void makeWalls(City city) {
        Wall[] walls = new Wall[19];
        walls[0] = new Wall(city, 1, 4, Direction.EAST);
        walls[1] = new Wall(city, 2, 4, Direction.EAST);
        walls[2] = new Wall(city, 3, 4, Direction.EAST)
        walls[3] = new Wall(city, 4, 4, Direction.EAST);
        walls[4] = new Wall(city, 5, 4, Direction.EAST);   
        walls[5] = new Wall(city, 1, 3, Direction.NORTH);
        walls[6] = new Wall(city, 1, 2, Direction.NORTH);
        walls[7] = new Wall(city, 1, 1, Direction.NORTH);
        walls[8] = new Wall(city, 1, 0, Direction.NORTH);
        walls[9] = new Wall(city, 1, 4, Direction.NORTH); 
        walls[10] = new Wall(city, 5, 4, Direction.SOUTH);
        walls[11] = new Wall(city, 5, 3, Direction.SOUTH);
        walls[12] = new Wall(city, 5, 2, Direction.SOUTH);
        walls[13] = new Wall(city, 5, 1, Direction.SOUTH);
        walls[14] = new Wall(city, 5, 0, Direction.SOUTH); 
        walls[15] = new Wall(city, 1, 0, Direction.WEST);
        walls[16] = new Wall(city, 2, 0, Direction.WEST);
        walls[17] = new Wall(city, 3, 0, Direction.WEST);
        walls[18] = new Wall(city, 4, 0, Direction.WEST);
        walls[19] = new Wall(city, 5, 0, Direction.WEST); 
    }
}

正确设置问题的格式我完全赞成使用空格来提高可读性,但代码的双空格只会使阅读变得更加困难。您描述的效果不是由于“代码顺序”,至少不是从这里显示的代码中可以看到的。我怀疑您有一个更严重的设计问题。例如,构造函数中的副作用。你需要显示墙、城市、机器人等的代码,让我们甚至能预感到这里发生了什么。不要管订单。尝试删除坐标与某个对象相同的任何墙。如果对象出现,则是图形顺序问题。