提问人:Habe44 提问时间:6/21/2023 更新时间:7/20/2023 访问量:73
如何检查char是否出现在ArrayList的字符串中?
How check if char appears in string in ArrayList?
问:
我正在写一个刽子手游戏,我需要一种方法来检查玩家输入的字母是否出现在计算机从 ArrayList 中提取的单词中。我需要为此使用流吗?我从未使用过流,但现在可能是时候了。为了更好地理解,我投入了我的代码。对象和字符串是波兰语。我想放在 createCipher 类中的方法
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Hangman {
static ArrayList<String> words = new ArrayList<>();
public static void main(String[] args) {
PrepareGame pomocnik = new PrepareGame();
PrepareGame.playerChoice pomocnik2 = new PrepareGame.playerChoice();
words.add("Puszka");
words.add("Kot");
words.add("Pies");
pomocnik.drawWord();
pomocnik.countLetters();
pomocnik.createCipher();
pomocnik2.choose();
}
}
class PrepareGame {
playerChoice player = new playerChoice();
String randomWords;
void drawWord() {
Random r = new Random();
int randomitem = r.nextInt(Hangman.words.size());
String randomW = Hangman.words.get(randomitem);
randomWords = randomW;
System.out.println(randomW);
}
void countLetters() {
int len = randomWords.length();
System.out.println(len);
}
void createCipher() {
for (int cypher = 0; cypher < randomWords.length(); cypher++) {
System.out.print("_");
}
}
static class playerChoice {
ArrayList<String> selectedLetter = new ArrayList<>();
String choice;
Scanner scan = new Scanner(System.in);
void choose() {
System.out.println(" ");
System.out.println("wpisz litere: ");
String pChoice = scan.nextLine();
choice = pChoice;
selectedLetter.add(choice);
System.out.println("Wybrana litera: " + choice);
}
}
}
我尝试使用indexOf()和contains()但没有成功。也许我用错了。
答:
"...我需要为此使用流吗?..."
否,可以使用 String#contains 或 String#indexOf 方法来确定 String 是否包含特定字符。
boolean found = randomWords.contains(choice);
boolean found = randomWords.indexOf(choice) != -1;
此外,对于这个游戏,您将需要确定特定字母的所有索引。
List<Integer> indices = new ArrayList<>();
int index = 0;
char selectedChar = choice.charAt(0);
for (char character : randomWords.toCharArray()) {
if (character == selectedChar) indices.add(index);
index++;
}
因此,如果你的单词是“Java”,选择是“a”,则索引列表将包含 1 和 3。
"...我从未使用过流,但现在可能是时候了。..."
是的,这是一个很好的方法——没有比简单地应用它更好的学习方法了。
如果您不熟悉这个话题,O'Reilly Media 有一本很棒的书,其中涵盖了整个主题。
O'Reilly Media – Java 8 Lambdas。
构建此流的方法很可能不止一种。
我正在使用 String#chars 方法返回一个 IntStream 对象。
从那里,我使用带有布尔谓词的 IntStream#anyMatch 方法来计算字符。
char selectedChar = choice.charAt(0);
boolean found
= randomWords.chars()
.anyMatch(character -> character == selectedChar);
使用流来映射索引有点棘手,我不得不查找它。
StackOverflow – Java 8/9:字符串中的字符可以映射到其索引(使用流)吗?
起点是整数值的范围,从 0 到 String 的长度。
从那里,他们流式传输范围,并且仅在匹配时添加字符。
下面是一个实现。
它利用 IntStream 类的范围、筛选器和装箱方法,以及 Stream#toList 方法。
char selectedChar = choice.charAt(0);
List<Integer> indices
= IntStream.range(0, randomWords.length())
.filter(value -> randomWords.charAt(value) == selectedChar)
.boxed()
.toList();
void createCipher() {
randomWords.chars()
.mapToObj(c -> (char) c)
.forEach(letter -> {
if (player.selectedLetter.contains(String.valueOf(letter))) {
System.out.print(letter);
} else {
System.out.print("_");
}
});}
评论
randomWords
List