我正在製作一個小型戰列艦遊戲。當我運行遊戲並模擬用戶獲勝時,即使用戶贏了,也要求獲得另外一組座標。在進入另一組座標後,遊戲接着說你贏了。戰列艦:一個輸入循環太多了
我似乎無法找出任何人可以幫助的問題?
感謝,
這裏是我的代碼
import java.util.Random;
import java.util.Scanner;
public class Battleships {
public static void main(String[] args) {
System.out.println("You'll have 10 tries to destroy the ship");
String[][] displayGrid = {
{ "O", "O", "O", "O", "O" },
{ "O", "O", "O", "O", "O" },
{ "O", "O", "O", "O", "O" },
{ "O", "O", "O", "O", "O" },
{ "O", "O", "O", "O", "O" }
};
String[][] internalGrid = {
{ "O", "O", "O", "O", "O" },
{ "O", "O", "O", "O", "O" },
{ "O", "O", "O", "O", "O" },
{ "O", "O", "O", "O", "O" },
{ "O", "O", "O", "O", "O" }
};
Random dice1 = new Random();
int r = 0;
r = dice1.nextInt(4);
Random dice2 = new Random();
int c = 0;
c = dice2.nextInt(5);
// Generates location of 2x1 ship
internalGrid[r][c] = "X";
internalGrid[r + 1][c] = "X";
// inserts random location into internal grid
for (int row = 0; row < displayGrid.length; row++) {
for (int col = 0; col < displayGrid[row].length; col++) {
System.out.print(displayGrid[row][col] + "\t");
}
System.out.println();
}
System.out.println();
int ucol = 0;
int urow = 0;
int hits = 0;
for (int row = 0; row < internalGrid.length; row++) {
for (int col = 0; col < internalGrid[row].length; col++) {
System.out.print(internalGrid[row][col] + "\t");
}
System.out.println();
}
//print internal grid for debugging
for (int i = 0; i < 10; i++) {
do {
Scanner row1 = new Scanner(System.in);
System.out.println("Enter a row between 1 and 5");
urow = row1.nextInt() - 1;
}
while (urow > 4 || urow < 0);
do {
Scanner col1 = new Scanner(System.in);
System.out.println("Enter a column between 1 and 5");
ucol = col1.nextInt() - 1;
}
while (ucol > 4 || ucol < 0);
if (internalGrid[r][c] == "H" && internalGrid[r + 1][c] == "H") {
System.out.println("You win!!");
i= 10;} //i think the issue is here??
else if (internalGrid[urow][ucol] == "X") {
System.out.println("Hit!!");
internalGrid[urow][ucol] = "H";
}
else if (internalGrid[urow][ucol] == "H") {
System.out.println("You already hit that location");}
else {
System.out.println("Miss :(");
}
}
System.out.println("Game Finished!!");
}
}
我的評論一樣,直到我注意的是,從上面的DO命令。 @CoderinoJavarino –
@JorgeCampos對不起,我不明白你能解釋一下你的意思嗎? –
還有另一個用戶評論說你的代碼什麼都不做,但是我們意識到它屬於它上面的'do'代碼,因爲它沒有很好的格式化,所以一眼就看不清楚。 –