You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检查玩家输入字符是否存在于ArrayList抽取的目标单词中?

问题描述

我正在编写Hangman(猜单词)游戏,需要实现一个方法,用于检查玩家输入的字母是否存在于电脑从ArrayList中抽取的单词里。我是否需要使用stream来实现?我从未使用过stream,但这次或许可以尝试。为便于理解,附上我的代码(对象和字符串为波兰语),希望将该方法放入PrepareGame类中。

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()方法,但未生效,可能是使用方式有误。


解决方案

1. 关于Stream的使用

完全没必要用Stream,String原生方法足够简单高效。但如果想学习Stream的用法,用它来实现也没问题,权当练手。

2. 原生方法失效的核心原因

大概率是大小写不匹配——比如抽取的单词是Puszka(首字母大写),玩家输入小写p,contains()会直接返回false。解决办法是统一转为小写(或大写)再做比较。

3. 两种实现方案(均放入PrepareGame类中)

方案一:String原生方法(推荐)

// 检查单个字母是否在目标单词中
boolean isLetterInWord(String letter) {
    // 统一转为小写,规避大小写差异问题
    return randomWords.toLowerCase().contains(letter.toLowerCase());
}

方案二:Stream实现(练手用)

boolean isLetterInWordWithStream(String letter) {
    String targetLetter = letter.toLowerCase();
    return randomWords.toLowerCase()
            .chars()
            .mapToObj(c -> String.valueOf((char) c))
            .anyMatch(targetLetter::equals);
}

4. 调用示例

修改playerChoice类,添加PrepareGame实例引用,在choose()方法中调用检查逻辑:

static class playerChoice {
    ArrayList<String> selectedLetter = new ArrayList<>();
    String choice;
    Scanner scan = new Scanner(System.in);
    PrepareGame game; // 添加游戏实例引用

    // 构造方法传入PrepareGame实例
    playerChoice(PrepareGame game) {
        this.game = game;
    }

    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);
        
        // 调用检查方法并输出结果
        boolean exists = game.isLetterInWord(choice);
        System.out.println(exists ? "Litera jest w słowie!" : "Litery nie ma w słowie!");
    }
}

同时修改main方法中playerChoice的初始化代码,传入PrepareGame实例:

public static void main(String[] args) {
    PrepareGame pomocnik = new PrepareGame();
    PrepareGame.playerChoice pomocnik2 = new PrepareGame.playerChoice(pomocnik);
    words.add("Puszka");
    words.add("Kot");
    words.add("Pies");

    pomocnik.drawWord();
    pomocnik.countLetters();
    pomocnik.createCipher();
    pomocnik2.choose();
}

内容的提问来源于stack exchange,提问作者Habe44

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 04:20:24