【问题标题】:app stops responding with no apparent reason应用无明显原因停止响应
【发布时间】:2013-05-03 02:35:49
【问题描述】:

我的骰子滚轮应用程序包含 7 个文本框(三对“骰子编号”和“骰子类型”以及一个奖励)和一个按钮。我打算单独读取每对文本框,如果它不包含有效数字('fate' 和 '%' 由于应用程序原因被读取为数字),它会忽略它。

问题是,当我没有在“否”之一中输入有效数字时。 of dice'文本框应用停止响应,并最终返回加载页面。

请注意,我已经分别测试了每种方法。

代码如下:

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;
    }
}

}

-提前感谢帮助

编辑:我按照 cmets 中的建议使用调试器查看了它(谢谢),错误是“值不能为空”。但有什么价值?它没有提供任何线索。再次感谢。

【问题讨论】:

  • 你在调试器下运行过吗?如果没有,我建议你从那里开始。
  • @MatthewWatson - 你打败了我,这听起来像是 Efften 先生和 EFFEleven 夫人的工作
  • Efften 先生和 EFFEleven 夫人,我喜欢这样。在任何情况下,这两个人都是他的好朋友。
  • 我可能会问,Effen 和 EFFEleven 是谁?

标签: c# windows-8 crash


【解决方案1】:

你已经创建了一个列表数组

List&lt;int&gt;[] results = new List&lt;int&gt;[3];

你真正想要的是 List&lt;int&gt;() results = new List&lt;int&gt;();

然后用results.Add(Roll());向它添加值

您需要进行更多调试,以确保您的最终文本集有 3 个值

编辑 2 这支持了这个理论

编辑..

刚刚意识到您有两种滚动方法, 你应该在设置它们之前初始化为 sucn

for(int i = 0; i < 3; i++)
{
results[i] = new List<int>();
}

【讨论】:

  • 你犯了一个错误:你写了“List() results = new List();”它应该在哪里:“List results = new List();”。谢谢你的回答
猜你喜欢
  • 1970-01-01
  • 2015-01-19
  • 2022-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
  • 1970-01-01
相关资源
最近更新 更多