所以我写了一个小Hang子手游戏,但我没有CLUE为什么它不工作。通常我会问一个问题来解决问题,但我根本不知道发生了什么,为什么我会遇到这个问题。没有错误。为什么我的代码不能按预期工作?
这里是我的所有代码:
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
String mysteryGuess = "hello";
String userGuess = "";
int wrongGuesses;
System.out.println("Hello and welcome to Hang Man!");
loop:
for(;;){
String[] wordAsArray = convertToStringArray(mysteryGuess);
boolean[] guessed = new boolean[mysteryGuess.length()];
for (int i = 0; i<wordAsArray.length;i++)
if(wordAsArray[i] == userGuess)
guessed[i]=true;
System.out.println("Word so far:" + visibleWord(wordAsArray,guessed));
System.out.println("What is your guess?");
userGuess = sc.next();
if (guess(userGuess,wordAsArray,guessed) == true)
System.out.println("Correct");
else
System.out.println("Incorrect");
if (didWin(guessed)==true)
break loop;
}
}
//This method creates an array version of the parameter word
//For example, if word contained the data "hello", then this method
//would return {"h", "e", "l", "l", "o"}
//Parameters: word - a single word
//Returns: an array containing each letter in word
public static String[] convertToStringArray(String word) {
String [] pWord = new String [word.length()];
for (int i = 0; i<pWord.length; i++){
pWord[i] = word.substring(i,i+1);
}
return pWord;
}
//This method determines whether the player has won the game of HangMan
//Parameters: guessed - array of boolean values
//Returns: true - if every value in guessed is true
// false - if at least one value in guessed is false
public static boolean didWin(boolean[] guessed) {
boolean bGuess = true;
loop:
for (int i = 0; i<guessed.length;i++){
if(guessed[i]==false){
bGuess = false;
break loop;
}
}
return bGuess;
}
//This method determines what portion of the hidden word is visible
//For example, if the parameters are as follows:
// wordAsArray: {"h", "e", "l", "l", "o"}
// guessed: {true, false, false, false, true}
//Then the method should return "h???o"
//Parameters: wordAsArray - the individual letters to be guessed
// guessed - array of boolean values; a true value means the corresponding letter has been guessed
//Returns: A string representing how much of the word has been guessed (unguessed letters are represented by ?'s)
public static String visibleWord(String[] wordAsArray, boolean[] guessed) {
String visibleWord="";
for(int i = 0; i<wordAsArray.length;i++){
if (guessed[i] == true)
wordAsArray[i]=wordAsArray[i];
if (guessed[i] == false)
wordAsArray[i]="?";
}
for(int i = 0; i<wordAsArray.length;i++){
visibleWord=visibleWord+wordAsArray[i];
}
return visibleWord;
}
//This method checks to see if a player has made a successful guess in the game of Hang Man
//For example, if the parameters are as follows:
// letter: "e"
// wordAsArray: {"h", "e", "l", "l", "o"}
// guessed: {true, false, false, false, true}
//Then the guessed array would be changed to:
// guessed: {true, true, false, false, true}
//And the method would return false
//Parameters: letter - the letter that the user has just guessed
// wordAsArray - an array of individual letters that are to be guessed
// guessed - array of boolean values; a true value means the corresponding letter has been guessed
//Returns: true - if letter matches an unguessed letter in wordAsArray
// false - otherwise
public static boolean guess(String letter, String[] wordAsArray, boolean[] guessed) {
boolean appearsAtLeastOnce=false;
for(int i = 0; i<wordAsArray.length;i++)
if(letter.equalsIgnoreCase(wordAsArray[i])){
guessed[i] = true;
appearsAtLeastOnce=true;
}
return appearsAtLeastOnce;
}
}
所以,当你输入你的猜测,即使它是正确的,它会说的不正确和“字至今”不会显示你知道它是正确的。有人请向我解释这里有什么问题吗?我相信它是一件简单的事情,我失踪了,但我已经在这上面待了好几个小时。谢谢。
此外,我使用“你好”只是为了测试。我正在寻找输入各种字典单词的列表。
IMO“你是比较引用类型,而不是基本类型的''==将无法正常工作”更正确的不是“你是比较字符串,而不是反对。所以'=='不会工作“。 – dorukayhan