java hangman swing_运行hangman(Java)的程序中的逻辑出错

因此,这是在这里添加到库中的另一个library子手问题.我的实体和边界类几乎全部完成,只不过有一个名为revealLetter()的方法,该方法用正确猜测的字母替换空格.它还会计算正确猜出的字母(如果有)的数量,并将该整数返回给驱动程序,以确定它是否未命中或命中.如果用户输入了错误的字母,revealLetter()将返回零,否则它将返回正确的字母数来确定正确的字母.我的问题是,尽管填写了正确的字母,revealLetter()始终返回零.我抛出了一些信号以隔离正在发生的事情,并且退出我的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

Make 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() + "

Bingo! 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大学课程,并且有严格的法律禁止复制不属于我的代码.因此,如果发生任何这种情况,您能用大多数英语来解释我做错了什么.我仍然想弄清楚代码,在逻辑上我只是被卡住了.提前致谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值