-2
我調試了我的代碼(下面),發現問題是Convert()方法從文本框轉換文本。但我如何解決這個問題?「輸入字符串格式不正確。」 for Convert()方法
我有類似的問題here的例子。
代碼:
//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);
}
哇...而不是正確使用隨機你瘋了所有帽子評論...請刪除它與問題無關,因爲它不是dailywtf這裏:) –
任何原因你的RollDice方法返回一個字符串,而不是一個數字? –
錯誤告訴你你需要知道的東西 - 你試圖將不是整數的東西轉換成整數。代碼無法做到這一點,所以它的錯誤。那個文本框的價值是什麼? –