阵列显示不正常 [英] Array not displaying properly
问题描述
这是学校家庭作业,但我恳求别人只是纠正我的code。我在这工作了两天,我想我有一切工作只是我不能让它作为一个二维数组工作,所以我设置这暂时只是为了试图理出头绪,但我挖掘自己更深的一个洞,我认为。
的赋值需要两个骰子被轧制36,000次,然后为每个总和的结果在右侧显示出来,并且在左边的两个骰子,像这样的在二维阵列的总和:
12 850
11 1020
10 1200
...
2 900
块引用>我已经得到了右侧立柱正确显示,但左栏将不会显示的金额,它只是显示System.Int32 []一帮倍。
这里的code:
随机兰特=新的随机();
const int的ARRAY_SIZE = 13;
常量双DICE_ROLLS = 36000;
INT总和= 0;
INT die1 = 0;
INT die2 = 0;INT [] =求和新INT [ARRAY_SIZE]INT []骰子= {2,3,4,5,6,7,8,9,10,11,12};的for(int i = 0; I< DICE_ROLLS;我++)
{
die1 = rand.Next(1,7);
die2 = rand.Next(1,7); 综上所述= die1 + die2;
总和[总和] + = 1;
}的for(int i = 2; I< sums.Length;我++)
{
Console.WriteLine({0,2} {1,8},骰子,款项由[i]);
}
解决方案既然你将使用一个二维数组,你会想要做这样的事情:
VAR兰特=新的随机();
常量双diceRolls = 36000;变种款项=新[,] {{2,0},{3,0},{4,0},{5,0},{6,0},{7,0},{8,0}, {9,0},{10,0},{11,0},{12,0}};对于(VAR I = 0; I< diceRolls;我++)
{
变种die1 = rand.Next(1,7);
变种die2 = rand.Next(1,7); VAR总和= die1 + die2;
总和[总和 - 2,1] + = 1;
}为(变量I = 0; I&下; sums.GetLength(0);我+ +)
{
Console.WriteLine({0,2} {1,8},数额[我,0],数额[1,1]);
}注1:
总和 - 2
。由于数组长度只有11,你需要从骰子值中减去2。 (0-10,而不是2-12)。注2:
sums.GetLength(0)
。如果你使用sums.Length
你会得到22
,因为其实有数组中的22种元素。你需要得到的长度0级
注3:既然你要处理的二维数组,你将有骰子的总和
总和[I,0]
,并在总数总和[1,1]
。This is homework assignment for school, but I'm begging for someone to just correct my code. I've been working at this for two days and I think I have everything worked out except I can't get it to work as a 2D Array, so I set this up temporarily just to try to figure things out, but I'm digging myself deeper into a hole I think.
The assignment requires that two dice be rolled 36,000 times and then the results for each sum be displayed on the right, and the sum of the two dice on the left, like this in a 2D array:
12 850
11 1020
10 1200
...
2 900I've got the right column displaying correctly, but the left column won't display the sums, it just displays "System.Int32[]" a bunch of times.
Here's the code:
Random rand = new Random(); const int ARRAY_SIZE = 13; const double DICE_ROLLS = 36000; int sum = 0; int die1 = 0; int die2 = 0; int[] sums = new int[ARRAY_SIZE]; int[] dice = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; for (int i = 0; i < DICE_ROLLS; i++ ) { die1 = rand.Next(1, 7); die2 = rand.Next(1, 7); sum = die1 + die2; sums[sum] += 1; } for (int i = 2; i < sums.Length; i++) { Console.WriteLine("{0,2} {1,8}", dice, sums[i]); }
解决方案Since you'll be using a 2d array you'll want to do something like this:
var rand = new Random(); const double diceRolls = 36000; var sums = new[,] {{2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0}, {8, 0}, {9, 0}, {10, 0}, {11, 0}, {12, 0}}; for (var i = 0; i < diceRolls; i++) { var die1 = rand.Next(1, 7); var die2 = rand.Next(1, 7); var sum = die1 + die2; sums[sum - 2, 1] += 1; } for (var i = 0; i < sums.GetLength(0); i++) { Console.WriteLine("{0,2} {1,8}", sums[i, 0], sums[i, 1]); }
Note 1:
sum - 2
. Since the array length is only 11 you need to subtract 2 from the dice value. (0-10 instead of 2-12).Note 2:
sums.GetLength(0)
. If you usesums.Length
you'll get22
since there actually are 22 elements in the array. You need to get the length forrank 0
Note 3: Since you're dealing with 2d arrays you'll have the sum of the dice roll in
sum[i, 0]
and the total count insum[i, 1]
.这篇关于阵列显示不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!