hey everyone i'm having troubles with a project, a try catch block. I need it to do the following:
First Check for first roll, if true skip hold checks and roll all dice
If false, Try the following ( 'lock' newly held dice (somehow) AND roll unheld dice )
If no dice are held, throw exception
i have attached my code. i'm so lost can someone help....
public partial class FrmBupkis1 : Form
{
private PictureBox[] diceImages;
private CheckBox[] holds;
private Random rnd = new Random();
public Frm1()
{
InitializeComponent();
diceImages = new PictureBox[6];
diceImages[0] = pbxDie0;
diceImages[1] = pbxDie1;
diceImages[2] = pbxDie2;
diceImages[3] = pbxDie3;
diceImages[4] = pbxDie4;
diceImages[5] = pbxDie5;
holds = new CheckBox[6];
holds[0] = chbHold0;
holds[1] = chbHold1;
holds[2] = chbHold2;
holds[3] = chbHold3;
holds[4] = chbHold4;
holds[5] = chbHold5;
}
private void rollBtn_Click(object sender, EventArgs e)
{
//First Check for first roll, if true skip hold checks and roll all dice
//If false, Try the following ( 'lock' newly held dice (somehow) AND roll unheld dice )
//If no dice are held, throw exception
for (int i = 0; i < 6; i++)
{
//if die is not held, then assign random number to image box
if (holds[i].Checked == false)
diceImages[i].Image = iglDice.Imag开发者_如何转开发es[rnd.Next(6)];
try
{
Random = random.Next(0, 6);
diceImages[i].Image = dieImages[Random];
rollBtn = true;
}
catch (FormatException ex)
{
Console.WriteLine(ex.Message, "Error. Try Again.");
}
}
}
private void gameOverBtn_Click(object sender, EventArgs e)
{
for (int i = 0; i < 6; i++)
{
diceImages[i].Image = null;
holds[i].Checked = false;
holds[i].Enabled = true;
}
}
private void quitBtn_Click(object sender, EventArgs e)
{
//btnQuit
this.Close();
}
}
There's many problems with this code:
Random = random.Next(0, 6);
diceImages[i].Image = dieImages[Random];
Assuming dieImages
should really be diceImages
(otherwise won't compile)
you are using a variable with the same name as a .NET class - confusing. Also this variable doesn't seem to be declared anywhere - just use the Random instance you have defined globally directly:
diceImages[i].Image = diceImages[rnd.Next(6)];
Also you were using a range from 0 to 6 - use the single parameter method Next
to get a range from 0 to 5 (upper boundary is exclusive) as shown above, otherwise you will get an IndexOutofRangeException
eventually when you access diceImages
.
I see where you are trying to catch an exception, but I don't see any place in your code where you are actually throwing an exception. Throwing an exception looks like this:
throw new ApplicationException("No dice are held");
Or you could create your own exception and throw it instead. The exception would be like this:
class NoDiceHeldException : ApplicationException
{
// define constructors here
}
and then you could throw the exception like this:
throw new NoDiceHeldException();
so i have this below for my try catch throw... still very confused on how it needs to work....ugh....
for (int i = 0; i < 6; i++)
{
//if die is not held, then assign random number to image box
try
{
if (holds[i].Checked == false)
diceImages[i].Image = iglDice.Images[rnd.Next(6)];
if (holds[i].Checked == true)
{
//HERE IS WHER I NEED IT TO HOLD THE DICE IF THEY ARE CHECK AND ONLY ROLL THE REMAINING DICE. UGH I'M STILL LOST...
}
throw new ApplicationException("No dice are held");
}
catch (Exception ex)
{
MessageBox.Show("You must hold at least 1 scoring dice before rolling", "Format Error");
}
}
I am still confused about what you are trying to do, but trying to help anyway. As you go through your loop from 0 to 5, you need to keep track of whether ANY of the items are held. You want to throw the exception after you are finished with the loop (i.e. below it). If you were to run the code you have now, for example, you would end up throwing the exception 6 times.
I think you will find that try/catch loop should be outside of your loop. It should be like this:
try
{
bool foundHeldDie = false;
for (int i = 0; i < 6; ++i)
{
// your code, which sets foundHeldDie to true if appropriate
if (holds[i].Checked == true)
{
foundHeldDie = true;
holds[i].Enabled = false;
}
}
if (!foundHeldDie)
{
// throw the exception
}
}
catch (Exception ex)
{
// handle the exception
}
To lock a dice (the place where you still have missing code), you could disable the checkbox (set the Enabled property to false), that way the user wouldn't be able to change it.
public partial class FrmBupkis1 : Form
{
private PictureBox[] diceImages;
private CheckBox[] holds;
private Random rnd = new Random();
public FrmBupkis1()
{
InitializeComponent();
diceImages = new PictureBox[6];
diceImages[0] = pbxDie0;
diceImages[1] = pbxDie1;
diceImages[2] = pbxDie2;
diceImages[3] = pbxDie3;
diceImages[4] = pbxDie4;
diceImages[5] = pbxDie5;
holds = new CheckBox[6];
holds[0] = chbHold0;
holds[1] = chbHold1;
holds[2] = chbHold2;
holds[3] = chbHold3;
holds[4] = chbHold4;
holds[5] = chbHold5;
}
private void rollBtn_Click(object sender, EventArgs e)
{
//First Check for first roll, if true skip hold checks and roll all dice
//If false, Try the following ( 'lock' newly held dice (somehow) AND roll unheld dice )
//If no dice are held, throw exception
for (int i = 0; i < 6; i++)
{
//if die is not held, then assign random number to image box
if (holds[i].Checked == false)
diceImages[i].Image = iglDice.Images[rnd.Next(6)];
}
try
{
bool foundHeldDie = false;
for (int i = 0; i < 6; ++i)
{
// your code, which sets foundHeldDie to true if appropriate
if (holds[i].Checked == true)
{
foundHeldDie = true;
holds[i].Enabled = false;
}
}
if (!foundHeldDie)
{
// throw the exception
throw new ApplicationException("No dice are held");
}
}
catch (Exception ex)
{
// handle the exception
MessageBox.Show("You must hold at least one die before rolling.", "Error");
}
}
private void gameOverBtn_Click(object sender, EventArgs e)
{
for (int i = 0; i < 6; i++)
{
diceImages[i].Image = null;
holds[i].Checked = false;
holds[i].Enabled = true;
}
}
private void quitBtn_Click(object sender, EventArgs e)
{
//btnQuit
this.Close();
}
}
精彩评论