2014-04-28 2 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

게다가 '추측'은 실제로 사용자가 넣은 문자 일 것 같아. 아마 메인 while() 루프 안의 글자를 비교하고 싶지 않을 것이다. . ''tries' 값을 확인한 다음, 루프 내에서 문자를 검색하십시오. 루프를 종료해야한다면 간단히 '중단'할 수 있습니다. –