2013-05-02 59 views
0

我的骰子應用程序包含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提前傭工

編輯:我看着它與調試器在評論中建議(感謝)錯誤是'值不能爲空'。但是什麼價值?它沒有給出任何線索。再次感謝。

+3

你在調試器下運行它嗎?如果沒有,我建議你從那裏開始。 – 2013-05-02 17:37:45

+3

@MthetheWWatson - 你打敗了我,這聽起來像Efften先生和EFFEleven夫人的工作 – Sayse 2013-05-02 17:41:22

+0

Efften先生和EFFEleven夫人,我喜歡那樣。在任何情況下,這兩個將是他的屁股。 – Zadam 2013-05-02 17:46:41

回答

1

你所做的名單

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

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

然後添加值,將它與results.Add(Roll());

你將有更多的調試數組確保最終文本集有3個值

編輯2 這支持了理論

enter image description here

編輯..

剛剛意識到你有2分輥塗法, 你應該將它們設置

for(int i = 0; i < 3; i++) 
{ 
results[i] = new List<int>(); 
} 
之前初始化爲sucn
+0

你犯了一個錯誤:你寫了「List ()results = new List ();」它應該是:「列表 results = new List ();」。謝謝你的答案 – user1461837 2013-05-03 16:45:08

相關問題