代码执行顺序的影响

时间:2015-03-25 19:15:12

标签: java methods

我遇到的问题是当我更改订单时代码停止工作。

例如,在以下代码中,除非它们是第一个,否则我的对象不会显示。我的机器人不会出现,除非它在顶部。如果我将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); 
    }
}

0 个答案:

没有答案