我的骰子滚轮应用程序包含 7 个文本框(三对“骰子编号”和“骰子类型”以及一个奖励)和一个按钮。我打算单独阅读每对文本框,如果它不包含有效数字('fate' 和 '%' 由于应用程序原因被读取为数字),它会忽略它。
问题是,当我没有在“否”之一中输入有效数字时。骰子的文本框应用程序停止响应,并最终返回到加载页面。
请注意,我已经分别测试了每种方法。
这是代码:
namespace DiceRoller
{
public sealed partial class MainPage : DiceRoller.Common.LayoutAwarePage
{
public MainPage()
{
this.InitializeComponent();
}
Random r = new Random();
//regular, untouched basic page code here
private void btnRoll1_Click(object sender, RoutedEventArgs e)
{
//the problem is with the number boxes.
List<int>[] results = new List<int>[3];
if (!(ReadInput(textBoxNumber1.Text) == 0 || ReadInput(textBoxType1.Text) == 0))
{
results[0] = Roll(ReadInput(textBoxType1.Text), ReadInput(textBoxNumber1.Text));
}
if (!(ReadInput(textBoxNumber2.Text) == 0 || ReadInput(textBoxType2.Text) == 0))
{
results[1] = Roll(ReadInput(textBoxType2.Text), ReadInput(textBoxNumber2.Text));
}
if (!(ReadInput(textBoxNumber3.Text) == 0 || ReadInput(textBoxType3.Text) == 0))
{
results[2] = Roll(ReadInput(textBoxType3.Text), ReadInput(textBoxNumber3.Text));
}
textBlockOutput1.Text = "Results:" + String.Join(", ",results[0]) + ", " + String.Join(", ", results[1]) + ", " + String.Join(", ", results[2]) + System.Environment.NewLine + "Total:" + ((results[0].Sum() + results[1].Sum() + results[2].Sum() + ReadInput(textBoxBonus.Text)).ToString());
}
//METHODS
private int ReadInput(string input) //tested
{
int returnValue = 0;
if (int.TryParse(input, out returnValue)) ; //the 'out' will make sure that the number has passed
else if (input == "%") returnValue = 100;
else if (input.ToLower() == "fate") returnValue = 6;
else if (input == "") ;
else textBlockOutput1.Text = "Error: All text boxes should contain a number, the strings '%', 'Fate'(not case sensitive) or to be blank";
return returnValue;
}
private int Roll(int diceType) //tested
{
return r.Next(diceType - 1) + 1;
}
private List<int> Roll(int diceType, int diceNumber)//tested
{
List<int> results = new List<int>();
for (int i = 1; i <= diceNumber; i++) results.Add(Roll(diceType));//if one of the no. textboxes is read as '0', this couln't operate
return results;
}
}
}
- 提前感谢帮助
编辑:我按照评论中的建议使用调试器查看了它(谢谢),错误是“值不能为空”。但有什么价值?它没有提供任何线索。再次感谢。