I have an integer array that I use for counting.
What is the easiest way to increase a certain index of the integer array by 1?
My code looks like this at the moment.
input = Convert.ToInt32(textBox1.Text);
number1 = rand.Next(1, 7);
number2 = rand.Next(1, 7);
number3 = rand.Next(1, 7);
//array used to keep track of how many times a certain number has showed up
int[] counters = new int[14];
int sum = number1 + number2 + number3;
for (int i = 0; i <= input; i++)
{
counters[sum] = counters[sum] + 1; //counters[sum]++;???
}
for (int i = 0; i < 14; i++)
{
richTextBox1.Text += "The number " + (i + 1) + "showed up" + counters[i] + " times\n";
}
What am I doing wrong? Why can't I do:
array[i]++; or array[i] = array[i] + 1; ???
EDIT for clarity.
What i am trying to do:
roll 3 dices as often as the user wants
sum the 3 dice rolls together (sum should be between 3 -18) and then keep a count of how often each sum showed up.
Then lastly display in % how many times each number showed up.
You can use linq and List to do this, see this code:
input = Convert.ToInt32(textBox1.Text);
for(var i = 0; i < input; i++)
{
number1 = rand.Next(1, 7);
number2 = rand.Next(1, 7);
number3 = rand.Next(1, 7);
//array used to keep track of how many times a certain number has showed up
int sum = number1 + number2 + number3;
sums.Add(sum); //Where sums is a List<int>
}
var report = sums.GroupBy(i => i).Select(g => new { TimesShowedUp = g.Count(), Sum = g.Key });
foreach(var r in report)
{
richTextBox1.Text += "The number " + r.Sum + "showed up" + r.TimesShowedUp + " times\n";
}
It sounds like what you actually want is the following.
Random rand = new Random();
int numberOfRolls = Convert.ToInt32(textBox1.Text);
int[] counters = new int[16]; //16 different possible sums (3 to 18)
for (int i = 0; i < numberOfRolls; i++)
{
int sum = rand.Next(1, 7) + rand.Next(1, 7) + rand.Next(1, 7);
counters[sum - 3]++;
}
for (int i = 0; i < 16; i++)
{
richTextBox1.Text += "The number " + (i + 3) + " showed up " + counters[i] + " times\n";
}
This take the number of times to roll from textBox1
then loops that number of times creating a sum
from the 3 random numbers (1-6). And increments a corresponding position in the array. Since there are 16 possibilities the array is initialed to that size and you just subtract 3 to convert from the sum to the index and then add 3 to convert back when printing.
//array used to keep track of how many times a certain number has showed up
int[] counters = new int[15];
iterations = Convert.ToInt32(textBox1.Text);
for (int i = 1; i <= iterations; i++)
{
number1 = rand.Next(1, 7);
number2 = rand.Next(1, 7);
number3 = rand.Next(1, 7);
int sum = number1 + number2 + number3;
counters[sum-3]++;
}
for (int i = 0; i <= 15; i++)
{
richTextBox1.Text += "The number " + (i + 3) + " showed up " + counters[i] + " times\n";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.