This question already has an answer here:
这个问题在这里已有答案:
- How do I compare strings in Java? 23 answers
如何比较Java中的字符串? 23个答案
So I have written a little Hangman game but I have NO CLUE why it is not working. Normally I would ask a question to solve a problem but I simply do not know what is going on and why I am having this problem. There isn't an error.
所以我写了一个小的Hangman游戏,但我没有CLUE为什么它不工作。通常我会问一个问题来解决问题,但我根本不知道发生了什么以及为什么我遇到这个问题。没有错误。
Here is all of my code:
这是我的所有代码:
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;
}
}
So when you input your guess even if it is right it will say its incorrect and the "word so far" wont show that you got it correct. May someone please explain to me what is wrong here? I am sure its a simple thing that I am missing but I have been on this for hours. Thank you.
因此,当你输入你的猜测,即使它是正确的,它会说它不正确,“到目前为止”这个词不会表明你弄错了。有人可以向我解释这里有什么问题吗?我确信这是一件我想念的简单事情,但我已经在这几个小时了。谢谢。
Also, I use "hello" just for testing. I am looking to input a list of various dictionary words.
另外,我只是用“hello”进行测试。我希望输入各种字典单词列表。
3 个解决方案
#1
3
Change
if(wordAsArray[i] == userGuess)
to
if(wordAsArray[i].equals(userGuess)) //Replace .equals with .equalsIgnoreCase for case insensitive compare.
You are comparing string, not object. So ==
won't work.
您正在比较字符串,而不是对象。所以==不会起作用。
Hope this helps.
希望这可以帮助。
#2
1
Your line if(wordAsArray[i] == userGuess)
is comparing two objects to see if they are equal. In this case two String objects. This comparison checks to see whether the references of the objects are equal (memory location). Even though the Strings have the same value, they are two different objects and thus have two different memory locations, which will make them unequal.
您的行if(wordAsArray [i] == userGuess)正在比较两个对象以查看它们是否相等。在这种情况下,有两个String对象。此比较检查以查看对象的引用是否相等(内存位置)。尽管字符串具有相同的值,但它们是两个不同的对象,因此具有两个不同的存储位置,这将使它们不相等。
What you are trying to do, is check to see whether the two objects values are the same. This can be accomplished by using String.equals() method: if(wordAsArray[i].equals(userGuess))
您要做的是检查两个对象值是否相同。这可以通过使用String.equals()方法来实现:if(wordAsArray [i] .equals(userGuess))
In general, when working with primitive data types (int, float, boolean, char...) the ==
comparison works just fine. However, when working with objects, you should be using the object's equals() method.
通常,在处理原始数据类型(int,float,boolean,char ...)时,==比较工作正常。但是,在处理对象时,您应该使用对象的equals()方法。
More on primitive types: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
有关原始类型的更多信息:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
#3
0
I have not reviewed your code deeply, but there are some issues about your code (and may be there are some more):
我没有深入审查您的代码,但是您的代码存在一些问题(可能还有一些问题):
- Most important ones is you are defining guessed
inside
forloop, so every time the array is initialized to all
false`. - You are using
==
for Strings, which won't work, you shall useString.equals()
method instead, by the way why didn't you usechar
s? -
java.lang.String
has atoCharArray()
which returns anchar[]
representing each character, and becausechar
is primitive you can use==
operator on it.
最重要的是你定义了guessedinsideforloop,所以每次将数组初始化为allfalse`。
你使用== for Strings,这将无法工作,你应该使用String.equals()方法,顺便说一下你为什么不使用chars?
java.lang.String有一个toCharArray(),它返回一个表示每个字符的char [],因为char是原始的,你可以在其上使用==运算符。
#1
3
Change
if(wordAsArray[i] == userGuess)
to
if(wordAsArray[i].equals(userGuess)) //Replace .equals with .equalsIgnoreCase for case insensitive compare.
You are comparing string, not object. So ==
won't work.
您正在比较字符串,而不是对象。所以==不会起作用。
Hope this helps.
希望这可以帮助。
#2
1
Your line if(wordAsArray[i] == userGuess)
is comparing two objects to see if they are equal. In this case two String objects. This comparison checks to see whether the references of the objects are equal (memory location). Even though the Strings have the same value, they are two different objects and thus have two different memory locations, which will make them unequal.
您的行if(wordAsArray [i] == userGuess)正在比较两个对象以查看它们是否相等。在这种情况下,有两个String对象。此比较检查以查看对象的引用是否相等(内存位置)。尽管字符串具有相同的值,但它们是两个不同的对象,因此具有两个不同的存储位置,这将使它们不相等。
What you are trying to do, is check to see whether the two objects values are the same. This can be accomplished by using String.equals() method: if(wordAsArray[i].equals(userGuess))
您要做的是检查两个对象值是否相同。这可以通过使用String.equals()方法来实现:if(wordAsArray [i] .equals(userGuess))
In general, when working with primitive data types (int, float, boolean, char...) the ==
comparison works just fine. However, when working with objects, you should be using the object's equals() method.
通常,在处理原始数据类型(int,float,boolean,char ...)时,==比较工作正常。但是,在处理对象时,您应该使用对象的equals()方法。
More on primitive types: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
有关原始类型的更多信息:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
#3
0
I have not reviewed your code deeply, but there are some issues about your code (and may be there are some more):
我没有深入审查您的代码,但是您的代码存在一些问题(可能还有一些问题):
- Most important ones is you are defining guessed
inside
forloop, so every time the array is initialized to all
false`. - You are using
==
for Strings, which won't work, you shall useString.equals()
method instead, by the way why didn't you usechar
s? -
java.lang.String
has atoCharArray()
which returns anchar[]
representing each character, and becausechar
is primitive you can use==
operator on it.
最重要的是你定义了guessedinsideforloop,所以每次将数组初始化为allfalse`。
你使用== for Strings,这将无法工作,你应该使用String.equals()方法,顺便说一下你为什么不使用chars?
java.lang.String有一个toCharArray(),它返回一个表示每个字符的char [],因为char是原始的,你可以在其上使用==运算符。