我正在尝试制作一个刽子手游戏。假设要猜的是"灯泡。"当用户猜出在单词中出现多次的字母时,在这种情况下是" l,"我想从所有位置的ArrayList中删除该字母,以便ArrayList包含" ightbub。"下面是我处理userGuesses的类的代码,并检查所选单词中的字母。以下代码:wordToGuess =("灯泡")
另一个问题:在此代码下方,我已经包含了输出。正如您所看到的,当我重复输入相同的字母时,它表示我已经猜到了一封信,即使该字母应该从ArrayList中删除。
import java.util.ArrayList;
public class InputChecker extends PromptUser{
static int numOfGuesses;
static String userGuess;
static String[] charList;
static boolean contains;
ArrayList<String> characters = new ArrayList<String>();
public boolean alive = true;
static WordSelector ws = new WordSelector();
static LengthReturner lr = new LengthReturner();
static PromptUser pu = new PromptUser();
static String wordToGuess = ws.setWord();
static int wordLength;
public static void finish() {
System.out.println();
System.out.println();
System.out.println("Congratulations! You have finished the game.");
System.out.println("Coming soon: GUI");
}
public boolean askUser() {
while(alive == true) {
boolean validGuess = false;
while(validGuess == false) {
userGuess = pu.getUserInput("Enter your guess here:");
if(userGuess == null || userGuess.length() > 1) {
validGuess = false;
System.out.println("Enter ONE letter!");
} else {
validGuess = true;
}
}
wordLength = lr.setLength(wordToGuess);
charList = wordToGuess.split("(?!^)");
contains = false;
for(String c : charList) {
characters.add(c);
}
for (String c : characters) {
if(userGuess == c) {
characters.remove(c);
contains = true;
}
}
if(contains == true) {
System.out.println("You guessed a letter!");
numOfGuesses++;
if(characters.isEmpty()) {
System.out.println();
System.out.println();
System.out.println("Congratulations! You have successfully guessed the word, which was " + wordToGuess + ".");
System.out.println("You took " + numOfGuesses + " guesses!");
alive = false;
}
} else {
System.out.println("Your guess, '" + userGuess + "', is not in the word!");
numOfGuesses++;
}
}
return alive;
}
}
这是我重复输入相同字母时的输出。注意它总是说我成功猜到了一封信,即使这封信应该从ArrayList中删除。不知何故,remove()不适合我。同样,wordToGuess =(&#34;灯泡:))
Enter your guess here:
l
You guessed a letter!
Enter your guess here:
l
You guessed a letter!
Enter your guess here:
l
You guessed a letter!
Enter your guess here:
l
You guessed a letter!
Enter your guess here:
l
You guessed a letter!
Enter your guess here:
l
You guessed a letter!
Enter your guess here:
l
You guessed a letter!
Enter your guess here:
l
You guessed a letter!
Enter your guess here:
l
You guessed a letter!
答案 0 :(得分:1)
移动线条:
charList = wordToGuess.split("(?!^)");
contains = false;
for(String c : charList) {
characters.add(c);
}
在while循环之外的。由于它在while循环中,它正在重新初始化&#34;字符&#34;在每次迭代中。
答案 1 :(得分:0)
Iterator.remove()
的JavaDoc(部分),
每次调用
next()
时,只能调用一次此方法。 迭代器的行为未指定,如果在迭代进行中修改基础集合,除了通过调用此方法之外的其他任何方式。
另外,您将void Main()
{
List<Music> myMusic = new List<Music>
{
new Music
{
Artist = "Mozart",
Album = "Mozarts amazing album",
TotalTracks = int.MaxValue,
Etc = int.MinValue
},
new Music
{
Artist = "Foo",
Album = "Bar",
TotalTracks = int.MaxValue,
Etc = int.MinValue
},
};
var mozartsMusic = myMusic.Where(music => music.Artist == "Mozart")
.ToList();
mozartsMusic.ForEach(Console.WriteLine);
}
public class Music
{
public string Artist { get; set; }
public string Album { get; set; }
public int TotalTracks { get; set; }
public int Etc { get; set; }
public override string ToString()
{
return string.Join("\n",this.GetType().GetProperties().Select(p=>string.Format("{0} {1}", p.Name, p.GetValue(this))));
}
}
种类型(包括Object
)平等与String
(不是equals()
)进行比较。你需要像
==