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

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

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

问题

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

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

  1. 我有一个数组:

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

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

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

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

  1. 输出:

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

  1. 然而我还想要输出名称。
  2. 示例:
  3. (在这个示例中,CASENAME只是占位符。我想使用CaseValue[].NAME,即使它不存在,因为这只是一个示例)

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

  1. 我期望的输出是:

CLUTCH_CASE: 8
CS20: 6
DANGER_ZONE_CASE: 6

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

for example, I have a list of integer variables

  1. int CLUTCH_CASE = 8;
  2. int CS20 = 6;
  3. int DANGER_ZONE_CASE = 6;
  4. int TOTAL_CASE = 0;

I have an array:

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

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

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

Output:

  1. You have 3 CSGO Cases
  2. 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)

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

My expected and wanted output to be:

  1. CLUTCH_CASE: 8
  2. CS20: 6
  3. DANGER_ZONE_CASE: 6

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

答案1

得分: 2

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

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

你可以这样做:

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

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

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

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:

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

After that you can do:

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

答案2

得分: 0

使用 Map。

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

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

英文:

Use a Map.

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

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() 方法,这样你可以将它放入一个类中,在程序中需要的时候进行调用。

  1. import java.lang.Class;
  2. import java.lang.reflect.*;
  3. Field[] varFset = ((java.lang.Class)this).getFields();
  4. // 在这里进行对数组的名称比较处理代码
  5. 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.

  1. import java.lang.Class;
  2. import java.lang.reflect.*;
  3. Field[] varFset = ((java.lang.Class)this).getFields();
  4. // compare names processing code over the array here
  5. String varname = varFset[x].getName();

答案4

得分: -1

你可以使用 HashMap。

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

You could use a HashMap.

  1. enum CaseType {
  2. CLUTCH_CASE,
  3. CS20,
  4. DANGER_ZONE_CASE;
  5. }
  6. Map&lt;CaseType, Integer&gt; map = new HashMap&lt;&gt;(3);
  7. map.put(CaseType.CLUTCH_CASE, 8);
  8. map.put(CaseType.CS20, 6);
  9. map.put(CaseType.DANGER_ZONE_CASE, 6);
  10. 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:

确定