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

如何调用方法处理数组中的统计数据并实现打印输出?

如何调用方法处理数组中的统计数据并打印输出?

看起来你正在做一个D&D职业生命骰子的统计工具,原代码有一些未完成的细节(比如未定义的变量、不完整的循环条件),我会帮你补全代码,并封装专门的统计方法来处理数组数据,同时展示如何调用这些方法输出清晰的统计结果。

先明确需求与完善基础代码

我们先补全原代码里缺失的变量(比如Scanner实例、index、characterClass),然后封装几个实用的统计方法:

  • 统计不同骰子值对应的职业数量
  • 计算骰子值的平均值、最大值、最小值
  • 根据选中的职业获取其骰子值并关联统计信息

完整示例代码

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;

public class CharacterStats {
    // 类级变量,方便各方法复用
    private static List<String> chaName = Arrays.asList("Wizard", "Sorcerer", "Monk", "Cleric", "Warlock", "Druid", "Rogue", "Bard", "Fighter", "Ranger", "Paladin", "Barbarian");
    private static int[] characterDiceValue = new int[]{6, 6, 8, 8, 8, 8, 8, 8, 10, 10, 10, 12};
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("Character Types");
        // 打印职业列表
        chaName.forEach(s -> System.out.println("\t" + s));

        // 1. 先调用全局统计方法,输出整体数据
        printDiceDistribution();
        printDiceStats();

        // 2. 引导用户选择职业,展示单个职业的关联统计
        String selectedClass = selectValidCharacterClass();
        int diceValue = getDiceByClass(selectedClass);
        System.out.printf("\n你选择的职业【%s】对应的生命骰子是d%d%n", selectedClass, diceValue);
        System.out.printf("当前共有%d个职业使用d%d骰子%n", getClassCountByDice(diceValue), diceValue);
    }

    // 统计每个骰子值对应的职业数量并打印
    private static void printDiceDistribution() {
        Map<Integer, Integer> diceCountMap = new HashMap<>();
        for (int dice : characterDiceValue) {
            diceCountMap.put(dice, diceCountMap.getOrDefault(dice, 0) + 1);
        }

        System.out.println("\n=== 生命骰子分布统计 ===");
        for (Map.Entry<Integer, Integer> entry : diceCountMap.entrySet()) {
            System.out.printf("d%d: %d个职业%n", entry.getKey(), entry.getValue());
        }
    }

    // 计算骰子值的基本统计量(平均、最大、最小)并打印
    private static void printDiceStats() {
        int sum = 0;
        int max = characterDiceValue[0];
        int min = characterDiceValue[0];

        for (int dice : characterDiceValue) {
            sum += dice;
            if (dice > max) max = dice;
            if (dice < min) min = dice;
        }
        double average = (double) sum / characterDiceValue.length;

        System.out.println("\n=== 生命骰子数值统计 ===");
        System.out.printf("最大值: d%d%n", max);
        System.out.printf("最小值: d%d%n", min);
        System.out.printf("平均值: %.2f%n", average);
    }

    // 引导用户选择有效职业,输入错误时重复提示
    private static String selectValidCharacterClass() {
        int index = -1;
        String selectedClass;
        do {
            System.out.println("\nSelect a Character name from the given list above");
            selectedClass = sc.next();
            index = chaName.indexOf(selectedClass);
            if (index == -1) {
                System.out.println("无效的职业名称,请重新选择!");
            }
        } while (index == -1);
        return selectedClass;
    }

    // 根据职业名称获取对应的骰子值
    private static int getDiceByClass(String className) {
        int index = chaName.indexOf(className);
        return characterDiceValue[index];
    }

    // 根据骰子值统计使用该骰子的职业数量
    private static int getClassCountByDice(int dice) {
        int count = 0;
        for (int d : characterDiceValue) {
            if (d == dice) count++;
        }
        return count;
    }
}

代码说明与方法调用逻辑

  1. 统计方法的封装:把不同的统计逻辑拆分成独立方法(比如printDiceDistribution、printDiceStats),让代码结构更清晰,也方便后续复用或修改统计规则。
  2. 方法调用时机:在main方法里,我们先调用全局统计方法输出整体数据,再引导用户选择职业,最后调用针对性方法展示单个职业的关联统计信息。
  3. 用户输入校验:selectValidCharacterClass方法完善了循环条件,确保只有输入有效职业名称时才会退出循环,避免程序出错。

运行这段代码后,你会先看到所有职业的列表,接着是骰子分布和数值统计,输入正确的职业名称后,就能看到对应的骰子值和该骰子的职业数量啦。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:01:51