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

Java中如何输出数组元素对应的原始变量名称而非变量值?

How to Output Integer Variable Names Alongside Their Values in Java?

Problem Description

I've defined several integer variables and an array to hold their values, and I've implemented logic to calculate and output the total number of cases. Here's my code:

int CLUTCH_CASE = 8; 
int CS20 = 6; 
int DANGER_ZONE_CASE = 6; 
int TOTAL_CASE = 0;

int CaseValue[] = {CS20, CLUTCH_CASE, DANGER_ZONE_CASE};

// Calculate total cases
for (int counter=0;counter<CaseValue.length;counter++) { 
    TOTAL_CASE += CaseValue[counter]; 
}

// Current output
System.out.println("You have "+CaseValue.length+" CSGO Cases");
System.out.println("You have a total of "+TOTAL_CASE+" CSGO Cases:");

The current output is:

You have 3 CSGO Cases
You have a total of 20 CSGO Cases:

But I also want to output the variable names alongside their values, like this:

CLUTCH_CASE: 8
CS20: 6
DANGER_ZONE_CASE: 6

Is there a way to achieve this?


Solution

Unfortunately, you can't directly retrieve the original variable names from an array in Java. When you compile your code, variable names are stripped out and replaced with memory references—so at runtime, the JVM has no idea what those variables were called in your source code.

Instead, you need to explicitly pair each variable's name with its value. Here are two straightforward ways to do this:

Option 1: Use a HashMap<String, Integer>

A HashMap lets you store key-value pairs, where the key is the variable name (as a string) and the value is the integer value. You can then iterate over the map to print both names and values:

import java.util.HashMap;
import java.util.Map;

public class CaseTracker {
    public static void main(String[] args) {
        int CLUTCH_CASE = 8; 
        int CS20 = 6; 
        int DANGER_ZONE_CASE = 6; 
        int TOTAL_CASE = 0;

        // Map case names to their values
        Map<String, Integer> caseMap = new HashMap<>();
        caseMap.put("CLUTCH_CASE", CLUTCH_CASE);
        caseMap.put("CS20", CS20);
        caseMap.put("DANGER_ZONE_CASE", DANGER_ZONE_CASE);

        // Calculate total
        for (int value : caseMap.values()) {
            TOTAL_CASE += value;
        }

        // Print summary
        System.out.println("You have "+caseMap.size()+" CSGO Cases");
        System.out.println("You have a total of "+TOTAL_CASE+" CSGO Cases:");
        
        // Print each case's name and value
        for (Map.Entry<String, Integer> entry : caseMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

Option 2: Create a Custom Case Class

If you want more structure (especially if you might add more properties later), define a simple class to hold both the name and value of each case:

// Custom class to represent a CSGO case with name and value
class Case {
    private String name;
    private int value;

    public Case(String name, int value) {
        this.name = name;
        this.value = value;
    }

    // Getters to access the name and value
    public String getName() {
        return name;
    }

    public int getValue() {
        return value;
    }
}

public class CaseTracker {
    public static void main(String[] args) {
        // Create Case objects with names and values
        Case clutchCase = new Case("CLUTCH_CASE", 8);
        Case cs20 = new Case("CS20", 6);
        Case dangerZoneCase = new Case("DANGER_ZONE_CASE", 6);
        int TOTAL_CASE = 0;

        // Store cases in an array
        Case[] cases = {clutchCase, cs20, dangerZoneCase};

        // Calculate total
        for (Case c : cases) {
            TOTAL_CASE += c.getValue();
        }

        // Print summary
        System.out.println("You have "+cases.length+" CSGO Cases");
        System.out.println("You have a total of "+TOTAL_CASE+" CSGO Cases:");
        
        // Print each case's name and value
        for (Case c : cases) {
            System.out.println(c.getName() + ": " + c.getValue());
        }
    }
}

Both options will produce the exact output you're looking for, along with your existing summary lines.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 20:42:40