Okay, so I'm having a number of issue with a tutorial question I'm working though so was hoping to get a bit of guidance as I'm very new to C# and not very good at it.
The scenario is this: Write a program that simulates the rolling of two dice 1000 times. The program should count the number of double sixes rolled and also output the average score of the 1000 dice rolls. You do not need to display the results of each of the 1000 rolls and need only hit the “Run Simulation” button once per 100 rolls. The button and output should be suitably labelled.
Although the scenario says to roll 1000 times and also to roll 2 dice at one, at present, I'm only doing it 10 times and one dice roll until I can get it that bit right. Here is the code I've written so far:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Dice_Roll_2___Advanced
{
public partial class Form1 : Form
{
int roll;
int countDouble;
int countRoll;
int count;
int count2;
int average;
int doubleSix;
public Form1()
{
InitializeComponent();
}
private void Dice_Roll_Click(object sender, EventArgs e)
{
Random rnd = new Random();
for(count = 0; count <1; count++)
{
for(count2 = 0; count2 < 10; count2++)
{
roll = rnd.Next (6) + 1;
MessageBox.Show(roll.ToString());
if(roll < 6)
{
countRoll++;
}
else if(roll == 6)
{
countDouble++;
}
}
}
}
private void Average_Click(object sender, EventArgs e)
{
average = (countRoll / 10);
Average.Text = average.ToString();
}
private void Doubles_Click(object sender, EventArgs e)
{
doubleSix = countDouble;
Doubles.Text = doubleSix.ToString();
}
Issue No.1
In the scenario, it says I have to output 100 numbers at once. As I'm only doing 10 at the moment, I wish to output 10 all at once. At present, that isn't happening. It gives me one number, I then click Ok, then it gives me the next number and so on. I believe this line in the code is the problem but not sure how to correct it.
MessageBox.Show(roll.ToString());
Issue No.2
The average does not work at all, it gives me a value of 0 every time. In my code you will see if wrote:
if(roll < 6)
{
countRoll++;
}
else if(roll == 6)
{
countDouble++;
}
then in the Label I wish to send the information: average = (countRoll / 10); Average.Text = average.ToString();
I know it's a lot of info but any help would be much appreciated.
A few pieces of advice:
Random
object is done at the class-level instead of in the Click
event handler of a button. Think about it this way: with your code the way it is now, the computer has to re-create the Random
object every time the user presses the Dice_Roll
button. This is a waste of system resources.int[]
or a List<int>
). Then, outside the loop that performs the rolls, make another loop that will build up the message you want to show.10
"magic number" into a constant (something like private const int numRolls = 10;
).average = (countRoll / 10);
makes no sense. You are dividing the number of rolls by 10 (which in this case...is the number of rolls). For an average, you need to add each roll up to get a total and then divide by the number of rolls.if
statements need some tweaking too. Currently the roll is only counted if it is < 6
meaning that a roll of 6
wont be counted, and will be left out of the average (probably not the behavior desired). countRoll
should be incremented every time a roll is made regardless of the value of the roll. Unfortunately this is all the feedback I have time to provide. Please don't feel like I'm trying to rip your code apart, I just wanted to point out some things that should get you back on track and clean up you code/logic a bit.
Best of luck.