英文:
Use loop variable to get another variable
问题
我创建一个文件,并且掷一个骰子,将结果保存在每一行。然后我读取文件,并将骰子掷出的点数保存为一个数组。
我遍历这个数组,统计我得到了多少个1、2、...、6。这些分别称为r1、r2、...、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'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't work. I have tried to search how to do this. Can'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
你在循环中使用了错误的运算符。`<` 应该是 `>`。
```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. <
vs >
.
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]; //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<=6; i++) {
String name = "r" + i;
int value = getClass().getDeclaredField(name).getInt(this);
System.out.println("The value of " + name + " is " + 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论