2016-07-04 26 views
0

我正在製作一個小型戰列艦遊戲。當我運行遊戲並模擬用戶獲勝時,即使用戶贏了,也要求獲得另外一組座標。在進入另一組座標後,遊戲接着說你贏了。戰列艦:一個輸入循環太多了

我似乎無法找出任何人可以幫助的問題?

感謝,

這裏是我的代碼

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!!"); 

} 

} 
+0

我的評論一樣,直到我注意的是,從上面的DO命令。 @CoderinoJavarino –

+0

@JorgeCampos對不起,我不明白你能解釋一下你的意思嗎? –

+0

還有另一個用戶評論說你的代碼什麼都不做,但是我們意識到它屬於它上面的'do'代碼,因爲它沒有很好的格式化,所以一眼就看不清楚。 –

回答

0

我懷疑它是做這一行就在這裏:

if (internalGrid[r][c] == "H" && internalGrid[r + 1][c] == "H") { 

我想你想用urowucol而不是rc ???

把一切都出:

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 :("); 

    } 

而與此替換:

boolean isAlreadyHit = (internalGrid[urow][ucol] == 'H'); 
boolean isNewHit = (internalGrid[urow][ucol] == 'X'); 

if (isAlreadyHit) { 
    System.out.println("You already hit that location");} 
} 
else if (isNewHit) { 
    System.out.println("Hit!"); 
    internalGrid[urow][ucol] = 'H'; 
} 
else { 
    System.out.println("Miss!"); 
} 

boolean isWinner = true; 
for (int row = 0; row < internalGrid.length; row++) { 
    for (int col = 0; col < internalGrid[row].length; col++) { 
     if (internalGrid[row][col] == 'X') { 
      isWinner = false; 
     } 
    } 
} 

if (isWinner) { 
    System.out.println("You win!"); 
    break; // breaks out of the outer for loop 
} 
+0

啊謝謝那個作品,我現在就坐下來弄明白它的工作原理以及爲什麼我的感謝 –

+0

如果你喜歡這個答案,你至少應該注意它,如果它適合你,你應該給它一個綠色的複選框(接受答案) – selbie

+0

對不起,我是新的到這個網站,我不知道你可以檢查它,我在你發佈後投了票,但它表示它不會公開展示,因爲聲譽需要高於15 –