Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 贝克尔机器人随机化墙壁_Java - Fatal编程技术网

Java 贝克尔机器人随机化墙壁

Java 贝克尔机器人随机化墙壁,java,Java,我在运行程序时遇到问题。该程序应该构建一个由墙组成的盒子,该盒子应该覆盖5-10条街道/大道,并且每次运行该程序时都具有不同的大小和位置。虽然有时候我在运行它的时候只得到1条大街和1条街道或者5条以下的东西?我错过了什么 public class CityWalls extends Thing { public CityWalls(City c, int st, int av, Direction d) { super(c, st ,av ,d); Random rand =

我在运行程序时遇到问题。该程序应该构建一个由墙组成的盒子,该盒子应该覆盖5-10条街道/大道,并且每次运行该程序时都具有不同的大小和位置。虽然有时候我在运行它的时候只得到1条大街和1条街道或者5条以下的东西?我错过了什么

public class CityWalls extends Thing {

public CityWalls(City c, int st, int av, Direction d) {
    super(c, st ,av ,d);

    Random rand = new Random();
    int randomNum = rand.nextInt(11);




    int oddIncrement = 0;
    if (randomNum % 2 == 0)
    {
        oddIncrement = 1;

    }


    for (int i = 0; i < randomNum; i++) { // creating the box. 7 is the placement of the robot so he appears in the middle of the box.

            new Wall(c, i+(7-randomNum/2), (7-randomNum/2), Direction.WEST);
            new Wall(c, i+(7-randomNum/2), (7+randomNum/2) - oddIncrement, Direction.EAST);
            new Wall(c, (7-randomNum/2), i+(7-randomNum/2), Direction.NORTH);
            new Wall(c, (7+randomNum/2)-oddIncrement, i+(7-randomNum/2), Direction.SOUTH);


    }
公共类城市墙扩展了{
公共城市墙(城市c、内街、内街av、方向d){
超级(c、st、av、d);
Random rand=新的Random();
int randomNum=rand.nextInt(11);
int=0;
如果(随机数%2==0)
{
ODD增量=1;
}
对于(int i=0;i <随机数;i++){//创建方框。7是放置机器人,所以他出现在盒子的中间。
新墙(c,i+(7-randomNum/2),(7-randomNum/2),方向为西;
新墙(c,i+(7-randomNum/2),(7+randomNum/2)-ODD增量,方向。东);
新墙(c,(7-randomNum/2),i+(7-randomNum/2),北向);
新墙(c,(7+randomNum/2)-ODD增量,i+(7-randomNum/2),方向为南部);
}
如果查看,您会发现
nextInt(int n)
返回一个介于
0
n
之间的值。您可能要做的是
5+rand.nextInt(6)
,因为这将确保范围是
[5,(5+6)[
而不是
[0,11[/code>

希望这有帮助。

使用:

int randomNum = 5 + (int)(Math.random() * ((10 - 5) + 1))
反而

Random rand = new Random();
int randomNum = rand.nextInt(11);

我很难弄清楚墙的创建是如何与大街和街道相关联的。不过有一个简单的问题:你是打算使用
(7-randomNum)/2
还是
7-(randomNum/2)
?目前,由于操作顺序的关系,它会使用后者。如果可以的话,我有一个后续问题?