有人能为我提供一种简单的方法,将第二个敌方战舰部署到我设计的只有一个敌人的游戏中吗,这是我目前的代码:
import java.util.*; //Scanner that reads user input
public static void main(String[] arg)
{
int riverLength = promptForInt("Select Length for River ") ; //Variable that stores the user input when prompted
int [] shipArray = new int[riverLength] ; //User input is used to create an Array, i.e. River Size
int battleshipCoordinates = new Random().nextInt(riverLength) ; //Random number is found within the Array (Where the enemy ship will hide)
shipArray[battleshipCoordinates] = 1 ;
boolean hit = false ; //Statement created for ship hit and default to false
int playerSelection; //int Variable declared
do
{
displayRiver (shipArray, false);
playerSelection = promptForInt(String.format("Select location to launch torpedo (1 - %d) ", riverLength));
playerSelection = playerSelection -1 ;
if(shipArray[playerSelection] == 1 ) //if a user strikes the Enemy (Case 1) correctly they system informs them of a strike
{
System.out.println("Battleship Sunk!");
hit = true;
displayRiver(shipArray, true);
}
else if(shipArray[playerSelection] == -1)
{
System.out.println("Location already Hit! Try again.");
}
else if(shipArray[playerSelection] == 0)
{
System.out.println("Miss!");
shipArray[playerSelection] = -1 ;
}
} while(!hit);
}
}
最佳答案
步骤0-创建一个新的战舰,并将其放置在阵列上,就像第一个一样。
第1步-将布尔值更改为等于2的整数。
第2步-而不是切换命中率,而是在命中船只时将其降低1
数组上的该位置为0。
步骤3-调整逻辑,除非打