所以這裏還有另一個hang子手問題添加到庫中。除了一個名爲revealLetter()的方法外,我的實體和邊界類都是完整的,它用正確猜測的字母替換空白。它還會計算正確猜測字母的數量(如果有的話),並將該整數返回給驅動程序以確定它是否觸發未命中或命中。如果用戶輸入錯誤的字母,revealLetter()將返回零,否則返回正確字母的數量以確定正確的字母。我的問題是,儘管填寫了正確的字母,revealLetter()總是返回一個零。我已經投入幾個sout來隔離發生了什麼事情,並且在退出我的for循環後,似乎計數器被設置爲零。我仍然在學習Java,所以很可能這很簡單,但目前對我來說似乎很複雜。下面是驅動程序:在運行hang子手(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大學課程,並有抄襲代碼嚴格的法律,不屬於我。因此,如果發生這種情況的任何一種靈魂,你能解釋我在大部分英語中做錯了嗎?我仍然想把代碼弄清楚,我只是邏輯上卡住了。在此先感謝
我大膽地提到了關於複製代碼的注意事項,所以人們不會意外忽略它並給你一個完整的解決方案。話雖如此,你是否曾嘗試在調試器中運行代碼,例如可能包含在IDE,如Eclipse,Netbeans或IntelliJ IDEA中的代碼? – hexafraction 2014-08-29 16:16:44
謝謝你那個傢伙!是的,我運行調試器(在Netbeans中),特別是在revelLetter()方法調用中,它似乎經歷了for循環,它會正確添加字母,但我不知道在for循環之後發生了什麼。如果我在返回correctChars上方放置一個sout語句,它會打印正確猜測字母的數量(不管它是1,2或3),但是它也會打印一個零。這導致我認爲有一個重置我的變量的第二次迭代。但我似乎無法孤立這個問題。 – 2014-08-29 16:31:53
我不能發現一個具體的問題,但我會建議簡化'revealLetter'中的if條件爲'(letter.charAt(0)== hiddenWord.charAt(i))',以揭示已經揭示的字母應該是無操作的。另外,爲什麼你的方法在class中是非靜態的,而'hiddenWord'和'partiallFoundWord'是靜態的? – hexafraction 2014-08-29 16:37:02