[任何人都可以为我提供一种简单的方法来在我设计的只有一个敌人的游戏中实施第二个敌方战舰的方法,这是我目前的代码:
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步,调整您的逻辑,除非打<1,否则您不会赢。>