What I'm trying do is to make program show how many times certain number has been generated. For example, if random number = 1 then +1 in the label displayed, if number 1 generated again then +1 to label which equal to 2 now and etc. Same with two and three, but in my case every time i click button , it would just display 1 in different labels and not display the sum of how many times they have been generated.... For each number i have separate label.
public partial class Form1 : Form
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Random gen = new Random();
int number = gen.Next(1, 4);
label5.Text = number.ToString();
if (number == 1)
{
int sum = 0;
int count = 1;
sum += count;
label9.Text = sum.ToString();
}
else if (number == 2)
{
int sum = 0;
int count = 1;
sum += count;
label10.Text = sum.ToString();
}
else if (number == 3)
{
int sum = 0;
int count = 1;
sum += count;
label11.Text = sum.ToString();
}
}
VulpesPosted Apr 29, 2012, 2:42 PM
VulpesPosted Apr 30, 2012, 4:43 AM
You could then use totals[3] to count the occurrences of the number 3 and so subtracting one would be unnecessary.
However, the trouble with this is that totals[0] would not be used and so you'd be allocating more memory than you need to. Whilst you can argue that this doesn't really matter in a small application, it goes against the grain with most developers - particularly those who remember what programming was like when memory was severely limited and every byte was important.
Prime bPosted Apr 29, 2012, 11:33 PM
:P
Prime bPosted Apr 29, 2012, 2:20 PM
totals is an array [ then it subtracts 1 from the random number] adds one?
VulpesPosted Apr 29, 2012, 1:29 PM