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

Java答题游戏开发:如何避免重复获取随机国家?

Java答题游戏:修复不重复随机国家获取逻辑

原代码的核心问题

  • 历史记录赋值顺序完全错误:比如getRandomCountryeasyName里的变量赋值逻辑颠倒,导致历史记录无法正确追踪前两次使用的国家。
  • 参数传递错误:getRandomCountrymediumName和getRandomCountryhardName调用工具方法时,传入了错误的历史记录变量(误用了easy难度的变量),甚至getRandomCountryhardName还传入了错误的HashMap集合(误用了medium难度的集合)。
  • 无效的循环判断条件:getRandomCountryName的while循环里额外判断前两次历史是否相等,这对排除重复新国家毫无意义,反而可能导致逻辑混乱。

修复后的完整代码

import java.util.HashMap;
import java.util.Random;

public class Countries {
    private HashMap<String, String[]> countrieseasy;
    private HashMap<String, String[]> countriesmedium;
    private HashMap<String, String[]> countrieshard;
    private String previousEasyCountry;
    private String secondPreviousEasyCountry;
    private String previousMediumCountry;
    private String secondPreviousMediumCountry;
    private String previousHardCountry;
    private String secondPreviousHardCountry;
    private Random random;

    public Countries() {
        countrieseasy = new HashMap<>();
        countriesmedium = new HashMap<>();
        countrieshard = new HashMap<>();
        previousEasyCountry = null;
        secondPreviousEasyCountry = null;
        previousMediumCountry = null;
        secondPreviousMediumCountry = null;
        previousHardCountry = null;
        secondPreviousHardCountry = null;
        random = new Random();
    }

    public void addCountryeasy(String name, String capital, String language, String continent, String currency) {
        String[] details = {name, capital, language, continent, currency};
        countrieseasy.put(name, details);
    }

    public void addCountrymedium(String name, String capital, String language, String continent, String currency) {
        String[] details = {name, capital, language, continent, currency};
        countriesmedium.put(name, details);
    }

    public void addCountryhard(String name, String capital, String language, String continent, String currency) {
        String[] details = {name, capital, language, continent, currency};
        countrieshard.put(name, details);
    }

    public String[] getCountryeasyDetails(String name) {
        return countrieseasy.get(name);
    }

    public String[] getCountrymediumDetails(String name) {
        return countriesmedium.get(name);
    }

    public String[] getCountryhardDetails(String name) {
        return countrieshard.get(name);
    }

    private String getRandomCountryName(HashMap<String, String[]> countries, String previousCountry, String secondPreviousCountry) {
        Object[] countryNames = countries.keySet().toArray();
        String countryName;
        do {
            int randomIndex = random.nextInt(countryNames.length);
            countryName = (String) countryNames[randomIndex];
            // 仅判断新国家是否与前两次的重复,同时处理null避免空指针
        } while ((previousCountry != null && countryName.equals(previousCountry)) 
                || (secondPreviousCountry != null && countryName.equals(secondPreviousCountry)));
        return countryName;
    }

    public String getRandomCountryeasyName() {
        // 先更新历史记录:把上一次的国家移到"前前次",再获取新国家,最后更新"上一次"记录
        secondPreviousEasyCountry = previousEasyCountry;
        String countryName = getRandomCountryName(countrieseasy, previousEasyCountry, secondPreviousEasyCountry);
        previousEasyCountry = countryName;
        return countryName;
    }

    public String getRandomCountrymediumName() {
        secondPreviousMediumCountry = previousMediumCountry;
        String countryName = getRandomCountryName(countriesmedium, previousMediumCountry, secondPreviousMediumCountry);
        previousMediumCountry = countryName;
        return countryName;
    }

    public String getRandomCountryhardName() {
        secondPreviousHardCountry = previousHardCountry;
        String countryName = getRandomCountryName(countrieshard, previousHardCountry, secondPreviousHardCountry);
        previousHardCountry = countryName;
        return countryName;
    }
}

关键修复说明

  • 修正历史记录更新逻辑:在获取新国家前,先将previous的值赋给secondPrevious,获取到新国家后,再把新国家赋值给previous,确保正确追踪最近两次使用的国家。
  • 修正参数传递:每个难度的方法调用工具方法时,传入对应难度的历史记录变量和HashMap集合,保证各难度的历史记录独立不混淆。
  • 简化循环判断条件:只保留判断新国家是否等于前两次历史国家的逻辑,同时增加null判断,避免第一次获取国家时出现空指针异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 18:05:18