簡體   English   中英

邏輯在運行hangman(Java)的程序中出錯

[英]Logic going awry in a program that runs hangman (Java)

所以這里還有另一個劊子手問題要添加到庫中。 我的實體和邊界類都是完整的,除了一個名為revealLetter()的方法,它用正確猜到的字母替換空格。 它還計算正確猜測的字母數(如果有的話),並將該整數返回給驅動程序,以確定它是否發生了未命中或命中。 如果用戶輸入了錯誤的字母,revealLetter()將返回零,否則它將返回正確的字母數以確定正確的字母。 我的問題是,盡管填寫了正確的字母,revealLetter()總是返回零。 我已經拋出一些sout來分隔發生的事情,並且在退出for for循環后計數器顯示為零。 我還在學習Java,所以很有可能它很簡單,但目前我看起來很復雜。 這是驅動程序:

package hangman;

import java.util.Scanner;

public class Hangman {

public static int NUMBER_MISSES = 5;

public static void main(String[] args) {

    String guessedLetter;
    WordHider hider = new WordHider();
    Dictionary dictionary = new Dictionary();

    Scanner Keyboard = new Scanner(System.in);
    hider.setHiddenWord(dictionary.getRandomWord());
    System.out.println(hider.getHiddenWord().length());
    System.out.println(hider.getHiddenWord());

    do {
        hider.wordFound();
        System.out.printf(hider.getPartiallyFoundWord() + "   Chances Remaing: %d \nMake a guess: ", NUMBER_MISSES);
        guessedLetter = Keyboard.nextLine();
        hider.revealLetter(guessedLetter.toLowerCase());
        if (hider.revealLetter(guessedLetter)== 0) {
            NUMBER_MISSES--;
            if (NUMBER_MISSES == 4) {
                System.out.println("Swing and a miss!");
            }
            else if (NUMBER_MISSES == 3) {
                System.out.println("Yup. That. Is. A. Miss.");
            }
            else if (NUMBER_MISSES == 2) {
                System.out.println("MISS! They say third time is a charm.");
            }
            else if (NUMBER_MISSES == 1) {
                System.out.println("Ouch. One guess left, think carefully.");
            }              
        } else {
            System.out.println("That's a hit!");
        }
        if (hider.wordFound() == true) {
          NUMBER_MISSES = 0;
        }
    } while (NUMBER_MISSES > 0);

    if ((NUMBER_MISSES == 0) && (hider.wordFound() == false)) {
        System.out.println("Critical Failure. The word was " + hider.getHiddenWord() + " try harder next time and you'll win.");
    } else if ((NUMBER_MISSES == 0) && (hider.wordFound() == true)) {
        System.out.println(hider.getHiddenWord() + "\nBingo! You win!");
    }

}

}

這是將.txt中的單詞存儲到數組並生成隨機單詞的類:

package hangman;

import java.util.Random;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Dictionary {

//Random randomizer = new Random();
private static String randomWord;
String[] dictionary = new String[81452];
private static String FILE_NAME = "dictionarycleaned.txt";

Dictionary() {
    int words = 0;
    Scanner infile = null;
    try {
        infile = new Scanner(new File(FILE_NAME));
        while (infile.hasNext()) {
            dictionary[words] = infile.nextLine();
            words++;

        }
        //System.out.println(dictionary[81451]);
    } catch (FileNotFoundException e) {
        System.err.println("Error opening the file " + FILE_NAME);
        System.exit(1);
    }

}

public String getRandomWord(){
  //randomWord = (dictionary[randomizer.nextInt(dictionary.length)]);  //Are either of these techniques better than the other?
  randomWord = (dictionary[new Random().nextInt(dictionary.length)]);
  return randomWord;    
}

}

這是包含revealLetter()的類,它還處理隨機字:

package hangman;

public class WordHider {

private static String hiddenWord;
private static String partiallyFoundWord;

WordHider() {

    hiddenWord = "";
    partiallyFoundWord = "";

}

public String getHiddenWord() {
    return hiddenWord;
}

public String getPartiallyFoundWord() {

    return partiallyFoundWord;

}

public void setHiddenWord(String newHiddenWord) {
    int charCount;
    hiddenWord = newHiddenWord;
    for (charCount = 0; charCount < hiddenWord.length(); charCount++) {
        partiallyFoundWord += "*";
    }

}

public int revealLetter(String letter) {
    int correctChars = 0;

    if (letter.length() < 1 || letter.length() > 1) {
        correctChars = 0;
        return correctChars;
    } else {

        String tempString = "";

        for (int i = 0; i < hiddenWord.length(); i++) {
            if ((letter.charAt(0) == hiddenWord.charAt(i)) && (partiallyFoundWord.charAt(i) == '*')) {                   
                correctChars++;
                tempString += Character.toString(hiddenWord.charAt(i));

            } else {
                tempString += partiallyFoundWord.charAt(i);



            }

        }
        partiallyFoundWord = tempString;           
    }

    return correctChars;
}

public boolean wordFound() {
    boolean won = false;
    if (partiallyFoundWord.contains(hiddenWord)) {
        won = true;
    }
    return won;
}

public void hideWord() {
    for (int i = 0; i < hiddenWord.length(); i++) {
        partiallyFoundWord += "*";
    }

}

}

值得注意的是,我正在參加CS大學課程,並且有一個嚴格的法律來復制不屬於我的代碼。 因此,如果發生任何形式的靈魂,你能解釋我在大多數英語中做錯了什么。 我仍然想把代碼弄清楚,我只是邏輯上卡住了。 提前致謝

在您的驅動程序main()您有:

hider.revealLetter(guessedLetter.toLowerCase());
if (hider.revealLetter(guessedLetter)== 0)

這就是為什么你得到一個成功的電話然后第二次沒有任何關系。 我可以突出一些風格問題,但一個重要的問題是:

if (letter.length() < 1 || letter.length() > 1) {
    correctChars = 0;
    return correctChars;
} else {

為什么不只是letter.length() != 1 ,並且因為correctChars已經初始化為零,所以不需要再次執行,因此可以刪除整個“then”部分, if變為letter.length() == 1

也:

tempString += Character.toString(hiddenWord.charAt(i));

和:

tempString += partiallyFoundWord.charAt(i);                                   

兩者都做同樣的事情,所以選擇一種風格或另一種風格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM