有没有办法我可以输出变量名而不是值?[JAVA]

huangapple go评论142阅读模式
英文:

Is there any way I can output the variable name instead of the value? [JAVA]

问题

例如我有一个整数变量的列表

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


我有一个数组:

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


然后我输出总箱数的值,结果将会是:

for (int counter=0;counter<CaseValue.length;counter++) {
TOTAL_CASE += CaseValue

0
+
网站访问量
;
}
System.out.println("你有 "+CaseValue.length+" 个CSGO箱子");
System.out.println("你总共有 "+TOTAL_CASE+" 个CSGO箱子:");


输出:

你有 3 个CSGO箱子
你总共有 20 个CSGO箱子:


然而我还想要输出名称。

示例:
(在这个示例中,CASENAME只是占位符。我想使用CaseValue[].NAME,即使它不存在,因为这只是一个示例)

for (int counter2=0;counter2<CaseValue.length;counter2++) {
System.out.println(CASENAME+": "+CaseValue[counter2]);
}


我期望的输出是:

CLUTCH_CASE: 8
CS20: 6
DANGER_ZONE_CASE: 6


有没有办法输出整数变量的名称呢?
英文:

for example, I have a list of integer variables

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

I have an array:

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

and I output the value of total cases, which is going to be:

for (int counter=0;counter&lt;CaseValue.length;counter++) {
	TOTAL_CASE += CaseValue
0
+
网站访问量
; } System.out.println(&quot;You have &quot;+CaseValue.length+&quot; CSGO Cases&quot;); System.out.println(&quot;You have a total of &quot;+TOTAL_CASE+&quot; CSGO Cases:&quot;);

Output:

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

yet I also want to output the name.

Example:
(CASENAME are just placeholders for this example. I wanted to use CaseValue[].NAME even that does not exist since it's an example)

for (int counter2=0;counter2&lt;CaseValue.length;counter2++) {
    System.out.println(CASENAME+&quot;: &quot;+CaseValue[counter2]);
}

My expected and wanted output to be:

CLUTCH_CASE: 8
CS20: 6
DANGER_ZONE_CASE: 6

Is there any way to output the name of the integer variable?

答案1

得分: 2

在Java中没有命令可以访问变量名。

你可以使用一个映射(就像其他答案中所述),但我也想提供一个简单的可能性,这个可能性不需要Java的高级特性。

你可以这样做:

String[] caseName = {"CS20", "CLUTCH_CASE", "DANGER_ZONE_CASE"};

之后你可以进行如下操作:

for (int i=0; i<CaseValue.length; i++) {
    System.out.println(caseName[i] + ": " + CaseValue[i]);
}
英文:

There is no command in Java to access the variable name.

You could use a map (like stated in other answers), but I'd also like to present a simple possibility that is possible without advanced features of Java.

What you could do is the following:

String[] caseName = {&quot;CS20&quot;, &quot;CLUTCH_CASE&quot;, &quot;DANGER_ZONE_CASE&quot;};

After that you can do:

for (int i=0; i&lt;CaseValue.length; i++) {
    System.out.println(caseName[i] + &quot;: &quot; + CaseValue[i]);
}

答案2

得分: 0

使用 Map。

Map<String, Integer> ma = new Hashmap();
for (int counter=0; counter<CaseValue.length; counter++) {
    TOTAL_CASE += CaseValue[counter];
    ma.put(<你的键>, <你的值>);
}

我仍然不完全理解你的代码。因此无法为你提供完整的工作代码片段。但是我已经添加了一个片段,展示了如何可能使用一个 Map。将键的名称作为你想要的名称,并将其对应的值存储在该键的映射中。

英文:

Use a Map.

Map&lt;String, Integer&gt; ma = new Hashmap()
for (int counter=0;counter&lt;CaseValue.length;counter++) {
    TOTAL_CASE += CaseValue
0
+
网站访问量
; ma.put(&lt;your_key&gt;, &lt;your_value&gt;) }

I still dont understand your code completely. So cannot give you a full working snippet. I have however added a snippet on how you could possibly use a map. Store the key as the name that you want and its corresponding value in that key of the map.

答案3

得分: 0

你需要使用 java.lang.Class 及其 getFields() 方法来返回一个 java.lang.reflect Field[] 类对象的数组。
以下将给你一个想法,如何使用 API 文档中更适合的一组方法来设计这个过程,可以使用 List 或 Set,并使用 contains() 方法,这样你可以将它放入一个类中,在程序中需要的时候进行调用。

import java.lang.Class;
import java.lang.reflect.*;

Field[] varFset = ((java.lang.Class)this).getFields();
// 在这里进行对数组的名称比较处理代码
String varname = varFset[x].getName();
英文:

You need to use java.lang.Class and its getFields() method to return an array of java.lang.reflect Field[] class object
Below will give you an idea of how, go take a look in the API docs for a better more suitable set of methods to design the process with a List or Set to use contains() method so you can put it in a class and call when you want in a program.

import java.lang.Class;
import java.lang.reflect.*;

Field[] varFset = ((java.lang.Class)this).getFields();
// compare names processing code over the array here
String varname = varFset[x].getName();

答案4

得分: -1

你可以使用 HashMap。

enum CaseType {
   CLUTCH_CASE,
   CS20,
   DANGER_ZONE_CASE;
}
Map<CaseType, Integer> map = new HashMap<>(3);
map.put(CaseType.CLUTCH_CASE, 8);
map.put(CaseType.CS20, 6);
map.put(CaseType.DANGER_ZONE_CASE, 6);
map.forEach((key, value) -> System.out.println(key.name() + ": " + value));
英文:

You could use a HashMap.

enum CaseType {
   CLUTCH_CASE,
   CS20,
   DANGER_ZONE_CASE;
}
Map&lt;CaseType, Integer&gt; map = new HashMap&lt;&gt;(3);
map.put(CaseType.CLUTCH_CASE, 8);
map.put(CaseType.CS20, 6);
map.put(CaseType.DANGER_ZONE_CASE, 6);
map.forEach((key, value) -&gt; System.out.println(key.name() + &quot;: &quot; + value));

huangapple
  • 本文由 发表于 2020年5月30日 13:02:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/62098041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定