2014-04-28 72 views
-3

我需要幫助我的代碼中找出以下錯誤:更換正確的 信用戶輸入的神祕文字。我的最後一個方法的功能是做到這一點,但 我不明白爲什麼它只會輸入一個正確的字母,而不是所有的正確猜測。此外,用戶擁有的最大嘗試次數是8次,但是當我編譯我的程序時,嘗試的次數會進入負面,我無法弄清楚我的最大嘗試循環出了什麼問題。提前謝謝!我很欣賞這一點。 **在這個程序中使用幾種方法是我的任務的一個要求!的Java程序劊子手

import java.util.*; 
import java.io.*; 

    public class Hangman { 
public static Scanner keyboard; 
public static final int MAXSIZE = 15000; 
public static final int NUM_GUESS = 8; 
public static final int WORD_LENGTH = 20; 
public static void main(String[] args) { 
keyboard = new Scanner(System.in); 
int random = pickrandom(MAXSIZE); 

    try { 
     // Set up connection to the input file 
     Scanner input = new Scanner(new FileReader("dictionary.txt")); 
     String [] dictionaryWords = new String [MAXSIZE]; 
     int usedsize = 0; 
     while (usedsize < MAXSIZE && input.hasNextLine()){ 
      dictionaryWords [usedsize] = input.nextLine(); 
      usedsize ++; 
      } 
     System.out 
     .println("    H A N G M A N. " + 
       "\n This is a word guessing game. " + 
       "\n A word will be selected at random and kept hidden. You will try to figure out the secret word by" + 
       "\n guessing letters which you think are in the word. " + 
       "\n You will guess one letter at a time. " + 
       "\n If the letter you guess is correct, the position(s) of the letter in the secret word will be shown. " + 
       "\n You will be allowed " + NUM_GUESS + " wrong guesses If you guess incorrectly " + NUM_GUESS+ " times, you lose the game. " + 
       "\n If you guess all of the letters in the word, you win." + 
       "\n\n Press enter to continue "); 
     keyboard.nextLine(); 
     clearScreen(); 
     String word = dictionaryWords[random]; 

     System.out.println(dictionaryWords[random]); 
     String blank = blankWord(word); 

     String decision; 
     //decision = keyboard.nextLine().toUpperCase(); 
     System.out.println("Word to guess :"); 
     System.out.println(blank); 
     int tries = NUM_GUESS; 

     System.out.println("Enter a letter to guess or 9 to quit"); 
     String guess = keyboard.next(); 
     do{ 

      //System.out.println(tries); 
     while (!guess.equals("9") || !(guess.equals(word) && tries >0)) 
       { 
       char letter = guess.charAt(0); 
       String guesses = ""; 
       guesses += letter; 
       if (word.indexOf(letter) < 0) { 
        tries--; 
        System.out.println("Incorrect letters tried: " + guess); 
        System.out.println("Number of guesses left: " + tries); 
        System.out.println("Enter a letter to guess or 9 to quit"); 
        guess = keyboard.next(); 
       } 
       else { 
        String correctWord = correctWord(guess, word, blank, letter); 
        System.out.println(correctWord); 
        System.out.println("Enter a letter to guess or 9 to quit"); 
        tries--; 
        System.out.println("Number of guesses left: " + tries); 
        guess = keyboard.next(); 
        tries--; 
        System.out.println("Number of guesses left: " + tries); 
       } 












      } 
     if (guess.equals("9")){ 
      System.out.println("Thanks for playing"); 

     } 
     if (guess.equals(word)){ 
      System.out.println("You won!"); 
     } 
     if(tries == 0){ 
      System.out.println("You have no more guesses left"); 
     } 
     System.out.println("Play again? Y/N"); 
     decision = keyboard.nextLine().toUpperCase(); 

     } while (decision.equals("Y")); 
     //System.out.println("Play again? Y/N"); 
     //decision = keyboard.nextLine().toUpperCase(); 
    } 
    catch (FileNotFoundException e) { 
     System.out.println("There was an error opening one of the files."); 
    } 


} 
//Clears screen after introduction 
private static void clearScreen(){ 
    for (int blanks = 0; blanks < 80; blanks++) { 
     System.out.println(); 
    } 
} 

// This method returns a randomly selected integer 
// between 0 and count-1 
public static int pickrandom(int count) { 
    Random generator = new Random(); 
    return generator.nextInt(count); 
} 

// Places asterisks in place of the letters in the word 
// Parameter is the string word. it holds mystery word 

public static String blankWord(String word) { 
    String blank = ""; 

    for (int i = 0; i < word.length(); i++) { 

     blank += " * "; 
    } 
    return blank; 
} 
//Replaces asterisks with correct guess 
    public static void fillWord(String blank,String word, char letter){ 
for (int i = 0; i <word.length() ; i++){ 
    if(word.charAt(i) == letter){ 
     blank.charAt(i); 

    } 

} 
    } 
    //Receives correct guesses and replaces the asterisks with correct letter 
    //Parameters is the mystery word 
    public static String newWord (String word){ 
String newWord = ""; 
if (newWord.length() > 0){ 
    newWord += word.charAt(0); 
    for (int i = 1; i <word.length(); i ++){ 
     newWord += word.charAt(0); 
    } 

} 
return newWord; 
    } 
    //Counts the number of missed guesses and replaces total count 
    //Parameters are the mystery word and the letter guesses from user 
    public static boolean alreadyGuessed(String word, String guess){ 
for (int i = 0 ; i< word.length() ; i++){ 
    if (word.charAt(i)== guess.charAt(0)){ 
     return true; 
    } 
} 
return false; 
    } 
     //Compares to see if the letter has already been guessed 
     //Parameters are the mystery word and the user's guess 
     public static boolean letterGuessed(String word, String guess){ 
    for (int i = 0 ; i< word.length(); i++){ 
    if (word.charAt(i) == guess.charAt(0)){ 
     return true; 
    } 
} 
return false; 
    } 
    //Replaces correct guess in blanks 
    public static String correctWord(String guess, String word, String blank, char letter){ 

    for (int i = 0; i < word.length(); i++) { 

     if (letter == word.charAt(i)){ 
      if (i >0){ 
     blank = blank.substring (0, i) + letter + blank.substring(i +1); 
      } 
      if (i ==0){ 
       blank = letter + blank.substring(1); 
      } 

     } 
    } 
    return blank; 
} 



} 
+0

至少你嘗試過什麼,但是這是一個非常長的代碼塊。你怎麼能期望我們經歷這一切並回答它 –

+0

最後一種方法是用來計算字母的去向。我只是把整個代碼放在需要更多關於我的程序的信息中。 – user3582249

+1

您也可以從相關的幾個方法開始,然後將整個程序放在下面的另一個塊中。 –

回答

0

你的邏輯倒退了。如果嘗試是-1,則:

while(... || (guess.equals(word) && (tries < 8) && (tries > 0)) { 
while(... || (guess.equals(word) && (-1 < 8) && (-1 > 0)) { 
while(... || (guess.equals(word) && true && false)) { 
while(... || !(false)) { 
while(... || true) { 
while(true) { 

換句話說,你的循環永遠無法終止,因爲tries檢查是錯誤的。

while (!guess.equals(word) && (tries > 0)) { 

可能是你想要的。

+0

謝謝你的幫助!我原本有你的代碼,但它仍然給我同樣的錯誤。我把它改回到你的建議,但我仍然遇到同樣的問題 – user3582249

+0

另外,我猜'guess'實際上就是用戶輸入的字符。你可能甚至不想比較主要的while()循環中的字母。只需檢查'try'值,然後在循環內搜索你的信件。如果你不得不終止循環,那麼你可以簡單地從它中斷出來。 –