Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.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代码中的Inifite循环_Java - Fatal编程技术网

Java代码中的Inifite循环

Java代码中的Inifite循环,java,Java,我的程序中有一个关于无限循环和错误响应的问题。在我的程序中,我试图为战舰游戏随机设置战舰,但我在放置战舰部分时遇到了问题。我已经编写了代码,但我遇到了两个问题,一个是我在某处有一个无限循环,但我不知道在哪里,下一个问题是这些片段没有正确地设置在网格上。我已经查看了这个代码很长一段时间了,我还没有找到一个修复程序。这是: public void placeAllShips() { int direction = (int) Math.random()*2 ; int p1 = 0

我的程序中有一个关于无限循环和错误响应的问题。在我的程序中,我试图为战舰游戏随机设置战舰,但我在放置战舰部分时遇到了问题。我已经编写了代码,但我遇到了两个问题,一个是我在某处有一个无限循环,但我不知道在哪里,下一个问题是这些片段没有正确地设置在网格上。我已经查看了这个代码很长一段时间了,我还没有找到一个修复程序。这是:

    public void placeAllShips() {
  int direction = (int) Math.random()*2 ; 
  int p1 = 0 ; 
  int p2 = 0 ; 
  for(int ships = 1 ; ships < 6 ; ships ++ ) { 
   p1 = (int)(Math.random()*10); 
   p2 = (int)(Math.random()*10);  
   if ( p1 !=0 && p2!= 0 && direction == 0 /* Horizontal Direction*/ ){ 
    for(int i= 0; i < ships ; i ++ ){ 
     while(board[p1][p2+i].hasShip() == true || p2 + i > 10 && p2 - i < 0 ){  
      randomize(p1,p2) ;
     }
    }
    for(int j = 0 ; j < ships ; j ++ ) {
     board[p1][p2+j].setHasShip(true) ; 
    }

   } 
   else if ( p1 !=0 && p2!= 0 && direction == 1 /*Vertical Direction*/ ){ 
    for(int i= 0; i < ships ; i ++ ){ 
     while(board[p1+i][p2].hasShip() == true || p1 + i > 10 && p1 - i < 0 ){  
      randomize(p1,p2) ;
     }
    }
    for(int j = 0 ; j < ships ; j ++ ) {
     board[p1+j][p2].setHasShip(true) ; 
    }

   }
  }
 } 

 public void randomize( int x , int y ) { 
  //Generates random numbers.
  x = (int)Math.random()*10 ;
  y = (int)Math.random()*10 ;
 }
public void placeAllShips(){
int方向=(int)Math.random()*2;
int p1=0;
int p2=0;
对于(int ships=1;ships<6;ships++){
p1=(int)(Math.random()*10);
p2=(int)(Math.random()*10);
如果(p1!=0&&p2!=0&&direction==0/*水平方向*/){
对于(int i=0;i10&&p2-i<0){
随机化(p1,p2);
}
}
对于(int j=0;j10&&p1-i<0){
随机化(p1,p2);
}
}
对于(int j=0;j

谢谢你的帮助

我怀疑无限循环是由于不了解Java中参数传递的工作原理造成的。请看以下代码:

// You're calling this if you're trying to use a point which is already taken
randomize(p1,p2) ;

public void randomize( int x , int y ) {
    //Generates random numbers.
    x = (int)Math.random()*10 ;
    y = (int)Math.random()*10 ;
}
除了使用
Random
的单个实例而不是
Math.Random()
会更干净之外,您的
randomize()
方法从根本上说并没有达到您期望的效果

调用
randomize(p1,p2)
时,将
p1
p2
的值复制到参数
x
y
中作为初始值。对
x
y
的更改不会更改
p1
p2
。。。因此,如果你进入这个循环,它将是无限的,因为
p1
p2
在每次迭代中都是相同的

首先,您可能应该将循环更改为:

// Put this *outside* the top level loop so you only create a single instance
Random random = new Random();

...

while(p2 + i > 10 || p2 - i < 0 || board[p1][p2+i].hasShip()) {
    p1 = random.nextInt(10);
    p2 = random.nextInt(10);
}
//将此*放在*顶层循环之外,以便只创建一个实例
随机=新随机();
...
而(p2+i>10 | | p2-i<0 | |板[p1][p2+i].haship()){
p1=随机。nextInt(10);
p2=随机。nextInt(10);
}
无论如何,这都不是一个完整的解决方案(代码中还有其他错误),但是每次尝试理解一个问题是很重要的


(接下来要考虑的是,您需要在一个点上检查船舶的所有值-您需要选择一个点,然后尝试船舶在该点将使用的所有正方形,如果失败,您需要重新开始,而不仅仅是尝试该值
i
的不同点)

我正在使用相同的方法,我遇到了与此相同的问题。该方法成功地放置了前几艘船,但只有有时代码才能正常运行。大多数情况下,它只是通过无限的坐标和方向组合,这似乎是荒谬的。它怎么可能经历这么多组合却找不到一个有效的呢

如果有帮助的话,这是我的代码

public void placeAllShips() {
    int dir = 0;
    int xCoord = 0;
    int yCoord = 0;
    boolean flag;
    boolean overlap;
    for (int i=0; i<5; i++) {
      flag = true;
      overlap = false;
      while (flag) {
        xCoord = (int)(Math.random()*(10)); //get a random x coordinate
        yCoord = (int)(Math.random()*(10)); //get a random y coordinate
        dir = (int)(Math.random()*(2)); //get a random direction, 0 = horizontal, 1 = vertical
        if ((cellArr[xCoord][yCoord].hasShip()==false)&&(((dir==0)&&((xCoord+i)<=9))||((dir==1)&&((yCoord+i)<=9)))) {
          for (int j=0; j<i+1; j++) {
            if ((dir==0)&&(cellArr[xCoord+j][yCoord].hasShip())) {
              overlap = true;
            }
            else if ((dir==1)&&(cellArr[xCoord][yCoord+j].hasShip())) {
              overlap = true;
            }
          }
          if (overlap==false) {
            flag = false;
          }
        }
        System.out.print("A");
      }
      System.out.println(xCoord+":"+yCoord+":"+dir);
      for (int k=0; k<i+1; k++) {
        if (dir==0) {
          cellArr[xCoord+k][yCoord].setHasShip(true);
        }
        else {
          cellArr[xCoord][yCoord+k].setHasShip(true);
        }
      }
    }
  }
public void placeAllShips(){
int dir=0;
int xCoord=0;
int yCoord=0;
布尔标志;
布尔重叠;

for(int i=0;iYour for loops seam OK,所以我认为问题出在while循环中。如果进行调试,您可以很容易地看到无限循环发生的位置。您是否尝试使用调试器来找出无限循环存在的位置?您的“随机化”方法是错误的(我想,因为我不知道您的完整用例)。您将x和y随机化,但这应该是p1和p2(我猜)?
int direction=(int)Math.random()*2;
与写入
int direction=0;
。最好将其替换为
int direction=(int)(Math.random()*2);
我对我的代码进行了更改,但出现了数组越界异常。我现在如何解决该问题???@user1810925:您的循环条件是在检查是否使用了有效索引之前检查数组值。我已为此编辑了答案,但我们无法一次解决此问题实际上,这不是堆栈溢出的设计目的。你需要仔细检查你的程序,确保你自己理解它的每一行。然后退一步,将它与你真正想要实现的东西进行比较。(我认为你需要重新设计一点,而不仅仅是摆弄小部分。)谢谢你,我已经完全重新设计了这个方法。只需要编写代码。