2017-09-28 99 views
2

我是java的初學者。我試圖模仿頭部第一本Java書中的單維陣列戰艦代碼。代碼沒有失敗,但我無法使其正常工作。例如:[1,2,3]是包含戰艦位置的數組。如果我猜測除1之外的任何數字,則顯示爲未命中。但是,如果我連續三次(長度的戰列艦)猜測它是殺人的。我無法弄清楚這個問題。單維陣列戰艦編號

請問你能否幫我解決這個問題。該代碼下面貼:

package battleship; 

public class battleship { 
    public int[] battleship = new int[3]; 
    public int numofhits = 0; 
    String result = "miss";//instance variables complete 

    //setter method to initialize the array which holds the battle ship location 
    public void setbattleshiploc(int startpos) { 
     int i = 0; 
     for(i = 0; i < battleship.length; i++) { 
      battleship[i] = startpos + i; 
      System.out.println(battleship[i]); 
     } 
     System.out.println("initialized array is: " + java.util.Arrays.toString(battleship)); 
    } 

    //getter method to print the set battleship array 
    public int[] getbattleshiploc() { 
     System.out.println("Battleship array is: " + battleship); 
     return battleship; 
    } 

    //checking whether user guess inside the battleship array location 
    public String guessloc(int guessnum) { 
     //int i = 0; 
     for(int cell : battleship) { 
      System.out.println("Guessed number is: " + guessnum + " array element: " + battleship[cell]); 
      System.out.println("cell: "+ cell + " ,battleship[cell]: " + battleship[cell]); 

      if(cell == guessnum) { 
       numofhits++;     
       if(numofhits == 3) { 
        result = "kill"; 
        return result; 
       }//if the hits are 3 then return kill indicating that no more battle ship is available 
       else { 
        result = "hit"; 
        return result; 
       }//end inner else    
      }//end outer if 

      else { 
       //numofhits++; 
       result = "miss"; 
       return result; 
      }//end the if-else 

     }//end for loop 

     return "finished"; 
    }//end function guessloc 

}//end class 



package battleship; 
import java.util.Scanner; 

public class gamelauncher { 
public Scanner[] reader;  


    public static void main(String[] args) { 
     String result = "miss"; 
     int numofguess = 0; 
     //int loopnum = 0; 
     battleship launchgame = new battleship();//launch the game 
     int startpos = (int) (Math.random() * 5);//generate random number between 0-4 
     //int[] location = {startpos, startpos + 1, startpos + 2};//initialize three consecutive array element 
     launchgame.setbattleshiploc(startpos);//set the array as the battleship location using setter 

     //display the battleship position 
     System.out.println("Battle shipt positions are: " + startpos +" ," + startpos+1 + " ," + startpos+2); 
     System.out.println("the battle ship array is: " + launchgame.getbattleshiploc()); 
     //int[] battleshiplocation = launchgame.getbattleshiploc(); 
     System.out.println(java.util.Arrays.toString(launchgame.getbattleshiploc())); 

     while(result != "kill") { 

      Scanner reader = new Scanner(System.in); // Reading from System.in 
      System.out.println("Enter a number: "); 
      int guess = reader.nextInt(); //get the user input integer 
      //reader.close();//close the scanner 
      //loopnum++; 

      numofguess++; 
      if(guess < 0 || guess > 7) { 
       System.out.println("Maximum space available is 7 units(0-6) and battleship length is 3 units, Please provide the location accordingly"); 
       continue; 
      }//close if loop and go to the loop execution if the guess is not within the limits 
      else { 
       result = launchgame.guessloc(guess); 
       System.out.println("response from guessing method: " + result); 
       //get the status(hit/miss/kill) back from guess location method 

       if(result == "kill") { 
        System.out.println("We have destroyed all the parts of battle ship and it took " + numofguess +" guesses" ); 
        break;//get out of the loop as we have destroyed everything 
       }//end kill 
       else if(result == "hit") { 
        System.out.println("You have destroyed " + launchgame.numofhits+" parts of batlleship, please continue"); 
        continue; 
       } 
       else { 
        System.out.println("It's a miss dumbo, try again"); 
        continue; 
       } 
      }//end outer else statement 
     }//end while loop 

    }//end main method 

}//end class 
+1

當錯過一個零件時,您不應該立即返回「錯過」,只有在錯過了所有零件的情況下才應該返回它。 – Berger

+0

如果這個例子的目標是展示如何處理數組,我會把這本書扔進垃圾箱。不要在這裏開玩笑。無論如何,不​​要將字符串與數字運算符(比如!=,==等)進行比較。而是使用string1.equals(string2) –

回答

0

我可以通過給你改變這個功能來幫助你。請嘗試自行修復其他代碼。它會給你重要的經驗。

public String guessloc(int guessnum) { 
    for(int i=0;i<battleship.length;++i) { 
     if(battleship[i] == guessnum) { //it's a hit 
      battleship[i] = -1; //you cant hit it again, so change it 
      if(++numofhits == 3) { 
       return "kill"; 
      }else 
       return "hit"; 
      } 
    } 
    return "miss"; //miss should be outside of the for loop 
} 
+0

我使用了提供的功能並做了以下更改 – captainkangaroo

+1

我使用了提供的功能並做了以下更改。 1)使用.equals進行字符串比較,如上所述2)將函數稍微改爲numofhits ++;如果(numofhits == battleship [i])。感謝您的建議,代碼工作。 – captainkangaroo

0

我相信問題是,當你比較字符串u的使用==替代.equals(),所以你的代碼應該喜歡這樣:

if(result.equals("kill")) 
+2

*改進*做''kill'.equals(result)'因爲null.equals(「string」)=> NullPointerException'而'string'.equals(null)=> false ':) – Manu

+0

一般來說,這是真的。但在他們的實施中,它很好。他們在所有情況下分配文字「殺死」/「打」。這些都將是對同一個對象的引用。 –

+0

@SotiriosDelimanolis我不知道,謝謝你的分享。 – nerfyies