也许这很简单,我很遗憾,但我似乎无法弄清楚为什么我的数组数组被覆盖的最后一个值被放入数组中。
这是我的代码:
import java.util.Arrays;
import class.ArgsProcessor;
public class Dice {
public static void main(String[] args) {
ArgsProcessor ap = new ArgsProcessor(args);
int D = ap.nextInt("How many dice will be thrown each time?");
int T = ap.nextInt("How many throws?");
int[] dieThrown = new int[D];
int [][] thrown = new int[T][D];
double randomNum;
for (int t = 0; t < T; t++){
for (int d = 0; d < D; d++){
randomNum = Math.random();
if (randomNum < 1.0/6.0)
dieThrown[d] = 1;
else if (randomNum < 2.0/6.0)
dieThrown[d] = 2;
else if (randomNum < 3.0/6.0)
dieThrown[d] = 3;
else if (randomNum < 4.0/6.0)
dieThrown[d] = 4;
else if (randomNum < 5.0/6.0)
dieThrown[d] = 5;
else
dieThrown[d] = 6;
}
System.out.println("On throw " + (t+1) + " we rolled:");
System.out.println("\t" + Arrays.toString(dieThrown));
thrown[t] = dieThrown;
}
System.out.println(Arrays.deepToString(thrown));
}
}
以下是3次投掷3个骰子的示例输出:
On throw 1 we rolled:
[3, 4, 2]
On throw 2 we rolled:
[1, 5, 4]
On throw 3 we rolled:
[6, 5, 3]
[[6, 5, 3], [6, 5, 3], [6, 5, 3]]
但是,我期待这样的事情:
On throw 1 we rolled:
[3, 4, 2]
On throw 2 we rolled:
[1, 5, 4]
On throw 3 we rolled:
[6, 5, 3]
[[3, 4, 2], [1, 5, 4], [6, 5, 3]]
如何让它添加正确的值?
答案 0 :(得分:2)
您正在多次向阵列dieThrown
添加相同的数组对象thrown
。通过d
for循环的每次迭代,您将覆盖同一数组中的值,因此最后一次迭代的值是剩余的值。 thrown
数组中充满了对dieThrown
引用的同一数组对象的引用。
在每次迭代中创建一个新数组,与之前的迭代数组分开。您可以通过移动dieThrown
数组的声明来执行此操作:
int[] dieThrown = new int[D];
到t
for循环体的第一行,在d
for循环之前。
答案 1 :(得分:1)
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=" + twitter_user_name)));
}catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/#!/" + twitter_user_name)));
}
是整个程序中的同一个对象。您使用对同一对象的引用填充dieThrown
数组,并在每次迭代中替换其内容。因此,您的所有thrown
条目基本上都指向同一个位置,并显示已分配给thrown
的最后一个内容。为避免这种情况,请使用
dieThrown
在你的外部循环中,这样每个元素都将是一个单独的数组,用于保存内容。
答案 2 :(得分:0)
你也可以摆脱dieThrown
。您可以直接将其添加到thrown
。以下是代码
public class dice {
public static void main(String[] args) {
int D = 3;
int T = 3;
int [][] thrown = new int[T][D];
double randomNum;
for (int t = 0; t < T; t++){
for (int d = 0; d < D; d++){
randomNum = Math.random();
if (randomNum < 1.0/6.0)
thrown[t][d] = 1;
else if (randomNum < 2.0/6.0)
thrown[t][d] = 2;
else if (randomNum < 3.0/6.0)
thrown[t][d] = 3;
else if (randomNum < 4.0/6.0)
thrown[t][d] = 4;
else if (randomNum < 5.0/6.0)
thrown[t][d] = 5;
else
thrown[t][d] = 6;
}
System.out.println("On throw " + (t+1) + " we rolled:");
System.out.println("\t" + Arrays.toString(thrown[t]));
//thrown[t] = dieThrown;
}
System.out.println(Arrays.deepToString(thrown));
}
}
注意我正在直接添加public static void main(String[] args) {
int D = 3;
int T = 3;
int [][] thrown = new int[T][D];
double randomNum;
for (int t = 0; t < T; t++){
for (int d = 0; d < D; d++){
randomNum = Math.random();
if (randomNum < 1.0/6.0)
thrown[t][d] = 1;
else if (randomNum < 2.0/6.0)
thrown[t][d] = 2;
else if (randomNum < 3.0/6.0)
thrown[t][d] = 3;
else if (randomNum < 4.0/6.0)
thrown[t][d] = 4;
else if (randomNum < 5.0/6.0)
thrown[t][d] = 5;
else
thrown[t][d] = 6;
}
System.out.println("On throw " + (t+1) + " we rolled:");
System.out.println("\t" + Arrays.toString(thrown[t]));
//thrown[t] = dieThrown;
}
System.out.println(Arrays.deepToString(thrown));
}
输出
thrown
答案 3 :(得分:0)
您正在重复使用dieThrown
每个循环,而不是每次迭代都创建一个新数组。你可以通过将dieThrown
的初始化移动到外循环的开头来解决这个问题(参见它在这里运行:IDEONE);或者,更好的解决方案是摆脱dieThrown
并直接更新thrown
数组。
您还可以使用java.util.Random#nextInt(int)
代替Math.random
来简化所有内容。
public static void main( String[] args ){
int D = 3;
int T = 4;
int [][] thrown = new int[T][D];
Random rand = new Random();
for (int t = 0; t < T; t++){
for (int d = 0; d < D; d++){
thrown[t][d] = rand.nextInt(6)+1;
}
System.out.println("On throw " + (t+1) + " we rolled:");
System.out.println("\t" + Arrays.toString(thrown[t]));
}
System.out.println(Arrays.deepToString(thrown));
}