我的骰子应用程序包含7个文本框(三对'骰子数'和'骰子类型'和一个奖金之一)和一个按钮。我打算每一对文本框都单独阅读,如果它没有包含有效数字('命运'和'%'由于应用原因被读作数字),它会忽略它。应用程序停止响应,没有明显的原因
问题是,当我没有输入有效数字的'没有。骰子'文本框的应用程序停止响应,并最终返回到加载页面。
请注意,我已经分别测试了每种方法。
这里是代码:
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;
}
}
}
-Thanks提前佣工
编辑:我看着它与调试器在评论中建议(感谢)错误是'值不能为空'。但是什么价值?它没有给出任何线索。再次感谢。
你在调试器下运行它吗?如果没有,我建议你从那里开始。 – 2013-05-02 17:37:45
@MthetheWWatson - 你打败了我,这听起来像Efften先生和EFFEleven夫人的工作 – Sayse 2013-05-02 17:41:22
Efften先生和EFFEleven夫人,我喜欢那样。在任何情况下,这两个将是他的屁股。 – Zadam 2013-05-02 17:46:41