使用循环变量获取另一个变量。

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

Use loop variable to get another variable

问题

我创建一个文件并且掷一个骰子将结果保存在每一行然后我读取文件并将骰子掷出的点数保存为一个数组

我遍历这个数组统计我得到了多少个12...6这些分别称为r1r2...r6都是整数类型

现在我想用这些整数(r1..r6)创建一个新的数组

我考虑了以下的方法

```java
int[] sums = new int[6];
for (int i = 0; i < sums.length; i++) {
    sums[i] = r(i + 1); // 获取r1、r2、...、r6
}

这显然是行不通的。我尝试了搜索如何做到这一点,但无法弄清楚。

可以帮忙吗?

注:我知道由于只有六个变量,我可以这样做:

sums[0] = r1;
sums[1] = r2;
...
sums[5] = r6;

但我想学点新东西,并且用循环来做(:

谢谢!


<details>
<summary>英文:</summary>

I create a file, and roll a dice which is saved on each row. Then I read the file, and save the dice rolls as an array.

I loop through the array and count how many 1,2,..,6 I get. These are called r1,r2,..r6 and are int&#39;s.

Now I want to create a new array with these ints (r1..r6).

I was thinking something like:

int [] sums = new int[6];
for (int i = 0; i < sums.length; i++) {
sums[i] = r(i+1); //get r1,r2,..,r6
}


This obviously doesn&#39;t work. I have tried to search how to do this. Can&#39;t figure it out.

Help?

note: I know since it is just six variables I can do 

sums[0] = r1;
sums[1] = r2;
...
sums[5] = r6;


But I want to learn something new and do it with a loop (:

Thank you!

</details>


# 答案1
**得分**: 1

你在循环中使用了错误的运算符。`&lt;` 应该是 `&gt;`。
```java
int[] sums = new int[6];
int[] R = new int[]{ r1, r2, r3, r4, r5, r6 };

for (int i = 0; i < sums.length; i++) {
    sums[i] = R[i]; // 获取 r1、r2、...、r6
}
英文:

You used wrong operator in the loop. &lt; vs &gt;.

int [] sums = new int[6];
int[] R = new int[]{ r1, r2, r3, r4, r5, r6 };

for (int i = 0; i &lt; sums.length; i++) {
sums[i] = R[i]; //get r1,r2,..,r6
}

答案2

得分: 1

基础:您可以在数组上进行循环。因此,让我们创建一个。

像这样定义一个数组 R

int[] R = new int[]{ r1, r2, r3, r4, r5, r6 };

现在在循环中使用它,如下所示:

sums[i] = R[i];
英文:

Basics: You can loop on arrays. So, let's create one.

Define an array R like:

int[] R = new int[]{ r1, r2, r3, r4, r5, r6 };

Now use it in the loop like:

sums[i] = R[i];

答案3

得分: 1

你可以通过反射来实现这个。以下是一个简单的示例:

import java.lang.reflect.*;

class Foo {
  private int r1, r2, r3, r4, r5, r6;

  void run() throws Exception {
    r1 = 11; r2 = 22; r3 = 33; r4 = 44; r5 = 55; r6 = 66;

    for(int i=1; i<=6; i++) {
      String name = "r" + i;
      int value = getClass().getDeclaredField(name).getInt(this);
      System.out.println("变量 " + name + " 的值为 " + value);
    }
  }

  public static void main(String[] args) throws Exception {
    new Foo().run();
  }
}

这假设变量是对象成员。按名称获取局部变量要困难得多。

这仅应该用于娱乐目的。使用索引变量名会导致速度变慢,容易出错,并且显示对 Java 工作原理的严重误解。

英文:

You can do this via reflection. Here's a simple example:

import java.lang.reflect.*;

class Foo {
  private int r1, r2, r3, r4, r5, r6;

  void run() throws Exception {
    r1 = 11; r2 = 22; r3 = 33; r4 = 44; r5 = 55; r6 = 66;

    for(int i=1; i&lt;=6; i++) {
      String name = &quot;r&quot; + i;
      int value = getClass().getDeclaredField(name).getInt(this);
      System.out.println(&quot;The value of &quot; + name + &quot; is &quot; + value);
    }
  }

  public static void main(String[] args) throws Exception {
    new Foo().run();
  }
}

This assumes the variables are object members. Getting local variables by name is way harder.

This should only ever be used for fun. Using indexed variable names is slow, error prone, and shows a gross misunderstanding of how Java works.

huangapple
  • 本文由 发表于 2020年10月20日 01:36:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64432518.html
匿名

发表评论

匿名网友

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

确定