Project Hangman game

huangapple go评论64阅读模式
英文:

Projekt Hangman game

问题

以下是翻译好的部分:

public static ArrayList<Character> getGuesses(ArrayList<Character> allGuesses, char input){

        for (int i = 0; i < allGuesses.size(); i++) {
            if (allGuesses.get(i) == input) {
                System.out.println("您已经使用过该字符!");
            } else {
                allGuesses.add(input);
            }
        }

    return allGuesses;

}
英文:

I am a beginner in Java and I am working right now with a small projekt, a hangman game. One of the functions I am working on right now is a method where it takes a char input, check if the input is already added to the list or not, if it is, a message will show up saying "You have already used that character!" and the user will have to guess again, otherwise the input will be added to the list. My issue right now is that nothing is happening, inputs are not added to the list at all.

This is what I have done so far:
Any advice/help would be appreciated!

public static ArrayList&lt;Character&gt; getGuesses(ArrayList&lt;Character&gt; allGuesses, char input){

        for (int i = 0; i &lt; allGuesses.size(); i++) {
            if (allGuesses.get(i) == input) {
                System.out.println(&quot;You have already used that character!&quot;);
            }else {
                allGuesses.add(input);
            }
        }


    return allGuesses;

}

答案1

得分: 1

你在每次迭代中都将输入字符添加到搜索集合中。你应该在搜索集合并未找到字符后再添加它。

for (int index = 0 ; index < allGuesses.size() ; ++index) {
  if (allGuesses.get(index) == input) {
     System.out.println("You already used that character!");
     return allGuesses;
  }
}
allGuesses.add(input);
return allGuesses;

然而,这可以通过使用集合的contains方法来简化,而无需使用循环。

if (allGuesses.contains(input)) {
  System.out.println("You already used that character!");
  return allGuesses;
}
allGuesses.add(input);
return allGuesses;

如果可能的话,考虑将allGuesses的类型切换为Set实现(例如HashSet)。Set似乎更适合你使用集合的方式,并且可以将这个方法简化为:

if (!allGuesses.add(input)) {
  System.out.println("You already used that character!");
}
return allGuesses;
英文:

You are adding the input character on each iteration as you search the collection. You should only add it after you have searched the collection and not found it.

for (int index = 0 ; index &lt; allGuesses.size() ; ++index) {
  if (allGuesses.get(index) == input) {
     System.out.println(&quot;You already used that character!&quot;);
     return allGuesses;
  }
}
allGuesses.add(input);
return allGuesses;

However, this can be simplified by using the Collection contains method such that you do not employ a loop.

if (allGuesses.contains(input)) {
  System.out.println(&quot;You already used that character!&quot;);
  return allGuesses;
}
allGuesses.add(input);
return allGuesses;

If possible, consider switching the type of allGuesses to a Set implementation (e.g. HashSet). A Set seems to better match how you are using your collection and allows you to reduce this method to...

if (! allGuesses.add(input)) {
  System.out.println(&quot;You already used that character!&quot;);
}
return allGuesses;

huangapple
  • 本文由 发表于 2020年10月16日 18:14:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64387200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定