为什么我的程序有时运行,但需要永远运行一些其他时间?

时间:2015-04-14 16:58:48

标签: java netbeans

我试图创建一个可以生成battleship板的程序。它运行完美并且有时会生成一块板,但在其他时候它会保持“运行”状态。我在mac上使用netbeans,如果这有用的话。 (你可能会注意到,我没有在我的代码中调用某些方法,因为这个程序还没有完成,它已经过了一半)这是我的程序:

Class BattlehipGame:

public class BattleshipGame {

    public static void main(String[] args) {

    gamemaker.shipMaker();
    }

}

类游戏制作者:

Public class gamemaker {
private static int numberOfShips;
 public static void numberOfShipsSetter(int shipNumber) 
 {
if (shipNumber<=5)
    {
        numberOfShips = shipNumber;
        gamemaker.shipMaker();
    }
 }

 public static void shipMaker()
 {
  ship aircraftCarrier = new ship();
  ship battleShip = new ship();
  ship cruiser = new ship();
  ship dest1 = new ship();
  ship dest2 = new ship();

  aircraftCarrier.sizeSetter(5);
 battleShip.sizeSetter(4);
 cruiser.sizeSetter(3);
 dest1.sizeSetter(2);
 dest2.sizeSetter(2);

 aircraftCarrier.OrientationSetter();
 battleShip.OrientationSetter();
 cruiser.OrientationSetter();
 dest1.OrientationSetter();
 dest2.OrientationSetter();

  aircraftCarrier.positionOfShip();
 battleShip.positionOfShip();
 cruiser.positionOfShip();
 dest1.positionOfShip();
 dest2.positionOfShip();

 for(int m=0; m<10; m++)
    {
        for(int g=0; g<10; g++)
    {
        System.out.print(position[g][m]+" ");
    }
    System.out.println("");
    }





 }


}

船级:

public class ship {

 private int size;
 private int shipid = 0;
 private int orientationOfShip;
public static final int[][] position = new int[10][10];
 public void sizeSetter(int shipsize)
 {
 if (shipsize<=5)
 {
    size= shipsize;
    shipid=shipsize;
 }
 else
 {
     size =  0;
 }
 }


public void OrientationSetter()//this method sets whether the ship is vertical or horizontal (horizontal is 1, vertical is 2)
{
int orientation =  1 + (int)(Math.random()*2);
 orientationOfShip = orientation;
}

public void positionOfShip()//method gives the ship its position and updates the array/board
{
    Random randomno = new Random();
    if (orientationOfShip==1)//if orientation is horizontal
    {
        int i = 0;
        int xposition = randomno.nextInt(10-size);//this gives the x position of leftmost block of the ship  
        int yposition = randomno.nextInt(9);//this gives the y position of leftmost block of the ship

        boolean foo = false;

        do
        {
            while (foo==false&&i<size)
            {

                if (position[xposition+i][yposition]!=0||xposition+size>10)//checks if the ship hits another ship or has parts outside the "board"
                    {
                        foo = true;
                    }

                   i++;
            }


            if (foo)//if foo is true (i.e if it hits another ship/spills) then generate another position for the ship
                {

                        xposition = randomno.nextInt(9-size)+1;
                        yposition = randomno.nextInt(9)+1;

                }

        } while(foo);

        for (int bar = 0; bar<size; bar++)
        {
        position[xposition+bar][yposition] = shipid;
        }
    }


    else if (orientationOfShip==2)//this is the vertical version of the if
      {
        int i = 0;
        int ypos = randomno.nextInt(10-size);  
        int xpos = randomno.nextInt(9);

        boolean isThere = false;

        do
            {
                while (isThere==false&&i<size)
                    {
                        if (position[xpos][ypos+i]!=0||ypos+size>10)
                            {
                                isThere = true;
                            }
                        i++;
                    }
                if (isThere)
                    {            

                        ypos = randomno.nextInt(9-size)+1;
                        xpos = randomno.nextInt(9)+1;

                    }
        } while(isThere);

        for (int incVar = 0; incVar<size; incVar++)
        {
        position[xpos][ypos+incVar] = shipid;
        }
     }


}

}

1 个答案:

答案 0 :(得分:0)

在positionOfShip()中,你需要在外部do循环的开头设置foo = false - 或者如果foo被设置为true,那么外部循环将永远运行。