gpt4 book ai didi

c# - 应用程序无缘无故地停止响应

转载 作者:行者123 更新时间:2023-12-02 14:10:54 27 4
gpt4 key购买 nike

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

问题是,当我没有在其中一个“no”中输入有效数字时。骰子'文本框应用程序停止响应,并最终返回到加载页面。

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

这是代码:

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

}

-提前感谢帮助者

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

最佳答案

您已经创建了一系列列表

List<int>[] results = new List<int>[3];

你真正想要的是 List<int>() results = new List<int>();

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

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

编辑2这支持了这个理论

enter image description here

编辑..

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

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

关于c# - 应用程序无缘无故地停止响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16344315/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com