2014-01-11 88 views
-1

所以我寫了一個小Hang子手遊戲,但我沒有CLUE爲什麼它不工作。通常我會問一個問題來解決問題,但我根本不知道發生了什麼,爲什麼我會遇到這個問題。沒有錯誤。爲什麼我的代碼不能按預期工作?

這裏是我的所有代碼:

public static void main(String[] args) { 
    Scanner sc = new Scanner (System.in); 
    String mysteryGuess = "hello"; 
    String userGuess = ""; 
    int wrongGuesses; 


    System.out.println("Hello and welcome to Hang Man!"); 
    loop: 
    for(;;){ 
     String[] wordAsArray = convertToStringArray(mysteryGuess); 
     boolean[] guessed = new boolean[mysteryGuess.length()]; 
     for (int i = 0; i<wordAsArray.length;i++) 
      if(wordAsArray[i] == userGuess) 
       guessed[i]=true; 
     System.out.println("Word so far:" + visibleWord(wordAsArray,guessed)); 
     System.out.println("What is your guess?"); 
     userGuess = sc.next(); 
     if (guess(userGuess,wordAsArray,guessed) == true) 
      System.out.println("Correct"); 
     else 
      System.out.println("Incorrect"); 
     if (didWin(guessed)==true) 
      break loop; 
    } 
} 


//This method creates an array version of the parameter word 
//For example, if word contained the data "hello", then this method 
//would return {"h", "e", "l", "l", "o"} 
//Parameters: word - a single word 
//Returns:  an array containing each letter in word 
public static String[] convertToStringArray(String word) { 
    String [] pWord = new String [word.length()]; 
    for (int i = 0; i<pWord.length; i++){ 
     pWord[i] = word.substring(i,i+1); 
    } 
    return pWord; 

} 


//This method determines whether the player has won the game of HangMan 
//Parameters: guessed - array of boolean values 
//Returns:  true - if every value in guessed is true 
//    false - if at least one value in guessed is false 
public static boolean didWin(boolean[] guessed) { 
    boolean bGuess = true; 
    loop: 
    for (int i = 0; i<guessed.length;i++){ 
     if(guessed[i]==false){ 
      bGuess = false; 
      break loop; 
     } 


    } 
     return bGuess; 
} 


//This method determines what portion of the hidden word is visible 
//For example, if the parameters are as follows: 
//  wordAsArray: {"h", "e", "l", "l", "o"} 
//  guessed: {true, false, false, false, true} 
//Then the method should return "h???o" 
//Parameters: wordAsArray - the individual letters to be guessed 
//    guessed - array of boolean values; a true value means the corresponding letter has been guessed 
//Returns:  A string representing how much of the word has been guessed (unguessed letters are represented by ?'s) 
public static String visibleWord(String[] wordAsArray, boolean[] guessed) { 
    String visibleWord=""; 
    for(int i = 0; i<wordAsArray.length;i++){ 
     if (guessed[i] == true) 
      wordAsArray[i]=wordAsArray[i]; 
     if (guessed[i] == false) 
      wordAsArray[i]="?"; 
    } 
    for(int i = 0; i<wordAsArray.length;i++){ 
     visibleWord=visibleWord+wordAsArray[i]; 
    } 
    return visibleWord; 
} 


//This method checks to see if a player has made a successful guess in the game of Hang Man 
//For example, if the parameters are as follows: 
//  letter: "e" 
//  wordAsArray: {"h", "e", "l", "l", "o"} 
//  guessed: {true, false, false, false, true} 
//Then the guessed array would be changed to: 
//  guessed: {true, true, false, false, true} 
//And the method would return false 
//Parameters: letter - the letter that the user has just guessed 
//    wordAsArray - an array of individual letters that are to be guessed 
//    guessed - array of boolean values; a true value means the corresponding letter has been guessed 
//Returns: true - if letter matches an unguessed letter in wordAsArray 
//   false - otherwise 
public static boolean guess(String letter, String[] wordAsArray, boolean[] guessed) { 
    boolean appearsAtLeastOnce=false; 
    for(int i = 0; i<wordAsArray.length;i++) 
     if(letter.equalsIgnoreCase(wordAsArray[i])){ 
      guessed[i] = true; 
      appearsAtLeastOnce=true; 
     } 
    return appearsAtLeastOnce; 



} 

}

所以,當你輸入你的猜測,即使它是正確的,它會說的不正確和「字至今」不會顯示你知道它是正確的。有人請向我解釋這裏有什麼問題嗎?我相信它是一件簡單的事情,我失蹤了,但我已經在這上面待了好幾個小時。謝謝。

此外,我使用「你好」只是爲了測試。我正在尋找輸入各種字典單詞的列表。

回答

3

變化

if(wordAsArray[i] == userGuess) 

if(wordAsArray[i].equals(userGuess)) //Replace .equals with .equalsIgnoreCase for case insensitive compare. 

你是比較字符串,而不是反對。所以==將不起作用。

希望這會有所幫助。

+0

IMO「你是比較引用類型,而不是基本類型的''==將無法正常工作」更正確的不是「你是比較字符串,而不是反對。所以'=='不會工作「。 – dorukayhan

0

我還沒讀過你的代碼深,但也有你的代碼中的一些問題(可能是有一些更多):

  1. 最重要的是要定義猜insideloop, so every time the array is initialized to all返回FALSE。
  2. 您正在使用==作爲字符串,這將不起作用,您應該使用String.equals()方法代替,順便說一句,爲什麼您不使用char s?
  3. java.lang.String有一個toCharArray()它返回char[]代表每個字符,並且因爲char是原始的,您可以使用==運算符。
1

你的線if(wordAsArray[i] == userGuess)是比較兩個對象,看看他們是否相等。在這種情況下,兩個String對象。此比較檢查以查看對象的引用是否相等(內存位置)。即使字符串具有相同的值,但它們是兩個不同的對象,因此具有兩個不同的存儲位置,這將使它們不相等。

你正在試圖做的是檢查兩個對象是否相同。這可以通過使用String.equals()方法來實現: if(wordAsArray[i].equals(userGuess))

一般來說,基本數據類型(整型,浮點,布爾,焦炭......)工作時,==比較工作得很好。但是,在使用對象時,應該使用對象的equals()方法。

更多關於基本類型:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

相關問題