如何检查ArrayList中的字符串是否出现char?

问题描述 投票:0回答:2

我正在编写一个 Hangman 游戏,我需要一种方法来检查玩家输入的字母是否出现在计算机从 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()但没有成功。也许我用错了。

java string arraylist char java-stream
2个回答
0
投票

“...我需要为此使用流吗?...”

不,您可以使用 String#containsString#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”,并且 choice“a”,则 indices 列表将包含 13

“...我从未使用过流,但这可能是时候了...”

是的,这是一个很好的方法——没有比简单应用它更好的学习方法了。

如果您不熟悉该主题,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:字符串中的字符可以映射到其索引(使用流)吗?

起点是一系列Integer值,从0String的长度。
从那里,他们流式传输范围,并且仅在匹配时添加一个字符。

这是一个实现。
它利用 IntStream 类的 rangefilterboxed 方法,以及 Stream#toList 方法。

char selectedChar = choice.charAt(0);
List<Integer> indices
    = IntStream.range(0, randomWords.length())
               .filter(value -> randomWords.charAt(value) == selectedChar)
               .boxed()
               .toList();

0
投票
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("_");
            }
        });}
© www.soinside.com 2019 - 2024. All rights reserved.