0
    private void Calculate_Click(object sender, RoutedEventArgs e)
    {

        int x1 = int.Parse(textBox1.Text);
        int x2 = int.Parse(textBox2.Text);
        int x3 = int.Parse(textBox3.Text);
        int x4 = int.Parse(textBox4.Text);
        int x5 = int.Parse(textBox5.Text);
        int x6 = int.Parse(textBox6.Text);
        int x7 = int.Parse(textBox7.Text);
        int x8 = int.Parse(textBox8.Text);
        int x9 = int.Parse(textBox9.Text);



        int[] totalScore = new int[8];
        totalScore[0] = x1;
        totalScore[1] = x2;
        totalScore[2] = x3;
        totalScore[3] = x4;
        totalScore[4] = x5;
        totalScore[5] = x6;
        totalScore[6] = x7;
        totalScore[7] = x8;
        totalScore[8] = x9;
        int sum = 0;
        foreach(int i in totalScore)
        {
            sum += totalScore[i];
        }

    }

我的 Windows 7.1 App SDK 中有这个。我正在做的是创建一个高尔夫应用程序。它应该接受这个人在每个洞上得到的分数,并将其添加到数组中。然后这个人可以点击计算按钮,它会添加他们的分数,但如果不是所有的点都被填满,我希望它能够工作。如果甚至一个地方都没有满,它会一直在我身上崩溃。

4

5 回答 5

0

尝试使用 Int32.TryParse 而不是 int.Parse(...) 方法。这样,如果文本框为空,从字符串到 Int 的转换不会导致异常;

int x1;
Int32.TryParse(textBox1.Text,out x1);

int x2;
Int32.TryParse(textBox2.Text, out x2);
...
于 2013-04-28T16:59:52.973 回答
0

如果你先把你TextBox的 's 放在一个数组中,这会变得简单得多:

private TextBox[] numberTextBoxes;

// Call this method at some point while setting up your UI
private void Initialize()
{
    this.numberTextBoxes = new TextBox[] 
    {
         textBox1, textBox2, ...
    };
}

private void Calculate_Click(object sender, RoutedEventArgs e) 
{ 
    int sum = this.numberTextBoxes.Sum(t => this.GetIntValue(t.Text));
    ...
}

private int GetIntValue(string text)
{
    int value;
    int.TryParse(text, out value);
    return value; // 0 if text is not parseable
}

这样,您根本不需要x1, x2, x3, ... 或totalScore变量。您可以在一行中计算总和。

于 2013-04-28T17:00:02.617 回答
0

那么你可以简单地写

  int sum = totalScore.Sum();

但是,您的“崩溃”的原因可能是对索引为 8 的元素的访问。请记住,数组索引从零到数组长度 - 1 因此,如果您有 9 个输入框,您的数组声明应该是

int[] totalScore = new int[9];

totalScore[0] = x1;
totalScore[1] = x2;
totalScore[2] = x3;
totalScore[3] = x4;
totalScore[4] = x5;
totalScore[5] = x6;
totalScore[6] = x7;
totalScore[7] = x8;
totalScore[8] = x9;
int sum = totalScore.Sum();

当然,应该验证从 textbox.text 中包含的字符串到整数的转换以避免无效输入。最好的方法是通过 Int32.TryParse 方法

int tempValue;
if(Int32.TryParse(textBox1.Text,out tempValue)
    totalScore[0] = tempValue;

.. 等等其他文本框。

于 2013-04-28T17:00:31.223 回答
0

0 是用于添加数字的中性元素。;)

if(button1 == empty)
  x1 = 0
else
  x1 = parse()

if(button2 == empty)
  x2 = 0
else
  x2 = parse()

...

于 2013-04-28T17:00:56.287 回答
0

请改用 TryParse。

   private void Calculate_Click(object sender, RoutedEventArgs e)
    {

        int x1 = 0;
        int.TryParse(textBox1.Text,out x1);
        int x2 = 0;
        int.TryParse(textBox2.Text, out x2);
        int x3 = 0;
        int.TryParse(textBox3.Text, out x3);
        int x4 = 0;
        int.TryParse(textBox4.Text, out x4);
        int x5 = 0;
        int.TryParse(textBox5.Text, out x5);
        int x6= 0;
        int.TryParse(textBox6.Text, out x6);
        int x7 = 0;
        int.TryParse(textBox7.Text, out x7);
        int x8 = 0;
        int.TryParse(textBox8.Text, out x8);
        int x9 = 0;
        int.TryParse(textBox9.Text, out x9);


        int[] totalScore = new int[8];
        totalScore[0] = x1;
        totalScore[1] = x2;
        totalScore[2] = x3;
        totalScore[3] = x4;
        totalScore[4] = x5;
        totalScore[5] = x6;
        totalScore[6] = x7;
        totalScore[7] = x8;
        totalScore[8] = x9;
        int sum = 0;
        foreach(int i in totalScore)
            {
            sum += totalScore[i];
            }

        }
    }
于 2013-04-28T17:04:42.513 回答