Java 为什么我的程序有时运行,但有时却要花很长时间运行?
我试图制作一个程序,生成一个电路板。它运行完美,有时会生成一个板,但有时会保持“运行”状态。如果有帮助的话,我在mac上使用了netbeans。(您可能会注意到,我在整个代码中没有调用某些方法,这是因为该程序尚未完成,已经完成了一半)以下是我的程序: 职业格斗游戏:Java 为什么我的程序有时运行,但有时却要花很长时间运行?,java,netbeans,Java,Netbeans,我试图制作一个程序,生成一个电路板。它运行完美,有时会生成一个板,但有时会保持“运行”状态。如果有帮助的话,我在mac上使用了netbeans。(您可能会注意到,我在整个代码中没有调用某些方法,这是因为该程序尚未完成,已经完成了一半)以下是我的程序: 职业格斗游戏: public class BattleshipGame { public static void main(String[] args) { gamemaker.shipMaker(); } } 职业
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("");
}
}
}
公共类游戏制作者{
私人船舶;
公共静态无效发货人编号(国际发货人编号)
{
如果(Stof of Stof of Stof()中的PosiNo.P>,您需要在外DO循环开始时设置Foo= false,或者如果FO被设置为true,则外循环将永远运行。您没有考虑下一艘船没有位置的情况。在这种情况下,它将尝试将其永久放置。
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;
}
}
}
}