我已经调试了我的代码(如下),发现问题是转换文本框中的文本的 Convert() 方法。但我如何解决这个问题?
我在这里有一个类似问题的例子。
代码:
//METHODS
static string RollDice (int dice)
{
Random roll = new Random();
int rollOutput = roll.Next(1, dice);
//NOTE: VERY IMPORTANT! EVERY TIME THE INPUT GOES INTO THIS
//METHOD REMEMBER TO INCREACE IT BY ONE!
//AND NEVER LET THE USER TO ROLL TO DICE QUICKLY ONE AFTER ANOTHER!
string rollResult = rollOutput.ToString();
return rollResult;
}
static void TwiceD20(int bonus, bool advantage)
{
string firstRollString = RollDice(21) + bonus;
string secondRollString = RollDice(21) + bonus;
int firstRoll = Convert.ToInt32(firstRollString);
int secondRoll = Convert.ToInt32(secondRollString);
switch(advantage)
{
case true:
if (firstRoll >= secondRoll)
{
MessageBox.Show(firstRollString);
}
else
{
MessageBox.Show(secondRollString);
}
break;
case false:
if (firstRoll <= secondRoll)
{
MessageBox.Show(firstRollString);
}
else
{
MessageBox.Show(secondRollString);
}
break;
}
}
//BUTTONS
private void btn1d20_Click(object sender, EventArgs e)
{
MessageBox.Show("Roll Result: " + RollDice(21));
}
private void btnAdv_Click(object sender, EventArgs e)
{
TwiceD20(Convert.ToInt32(textBox1d20.Text), true);
}
private void btnDisAdv_Click(object sender, EventArgs e)
{
TwiceD20(Convert.ToInt32(textBox1d20.Text), false);
}