我正在制作一个程序,在文本框中添加用逗号(,)分隔的列表编号。 例如:1,12,5,23 在我的总数+ = num;我一直使用总共未使用的本地变量;获取未分配的变量错误的使用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str = textBox1.Text;
char[] delim = { ',' };
int total;
int num;
string[] tokens = str.Split(delim);
foreach (string s in tokens)
{
num = Convert.ToInt32(s);
total += num;
}
totallabel.Text = total.ToString();
}
}
}
我不敢相信我错过了,谢谢。这就是为什么当我疲倦的时候我不这样做。 – 2012-07-27 14:07:02