C# 变量不累加其他变量值

C# 变量不累加其他变量值,c#,winforms,streamreader,C#,Winforms,Streamreader,我试图读取文件中的每一行,并获取特定的字符串和整数。然而,找到的整数的值并没有在最后加起来,我不确定为什么。如果这是一个简单的错误,我道歉 如果文件中的行包含“事件类型:音乐”,请使用MusicTrace将“音乐”存储在事件类型[]数组中。音乐跟踪从0开始,并在每次找到上面的字符串时递增。因此,它沿着阵列工作。数组大小是文件中确保始终有足够数组空间的行数 我有另一个名为eventAttention[]的考勤数组,它执行与上面相同的步骤,但从找到的行中剪切出前18个字符,给出剩余的数字(文件中的行

我试图读取文件中的每一行,并获取特定的字符串和整数。然而,找到的整数的值并没有在最后加起来,我不确定为什么。如果这是一个简单的错误,我道歉

如果文件中的
包含
“事件类型:音乐”
,请使用
MusicTrace
“音乐”
存储在
事件类型[]
数组中。音乐跟踪从0开始,并在每次找到上面的字符串时递增。因此,它沿着阵列工作。数组大小是文件中确保始终有足够数组空间的行数

我有另一个名为
eventAttention[]
的考勤数组,它执行与上面相同的步骤,但从找到的行中剪切出前18个字符,给出剩余的数字(文件中的行是固定长度)
AttendanceTrace
的使用方式与上述
MusicTrace
相同

然后,我有一个EventAttention数组的循环,它使用
I
,从
0
开始执行代码,直到达到
EventAttention.Length
属性。代码使用
i

代码如下:

private void frmActivitiesSummary_Load(object sender, EventArgs e)
{
    if (File.Exists(sVenueName.ToString() + ".txt"))
    {
        using (StreamReader RetrieveEvents = new StreamReader(sVenueName.ToString() + ".txt"))                    //Create a new file with the name of the username variable
        {
            string[] ReadLines = File.ReadAllLines(sVenueName + ".txt");            //Read File

            int MusicTrace = 0;
            int AttendanceTrace = 0;
            string[] EventType = new string[ReadLines.Length];      //Store found event types
            int[] EventAttendance = new int[ReadLines.Length];      //Store Event Attendance

            string line;                                                            //Declare String to store line

            using (StreamReader file = new StreamReader(sVenueName + ".txt"))       //Using StreamReader
            {
                while (!file.EndOfStream)
                {
                    line = file.ReadToEnd();

                    //Get All Music Event to Array
                    if (line.Contains("Event Type: Music"))
                    {
                        EventType[MusicTrace] = "Music";        //[0] = Music
                        if (MusicTrace != 0)
                            MusicTrace = MusicTrace + 1;
                        else
                            MusicTrace = 1;
                    }

                    //Get All attendances to Array
                    if (line.Contains("People Attending:"))
                    {
                        line.Remove(0, 18);
                        int ConvertedLine = Convert.ToInt32(line);
                        EventAttendance[AttendanceTrace] = ConvertedLine;   //[0] = 10
                        if (AttendanceTrace != 0)
                            AttendanceTrace = AttendanceTrace + 1;
                        else
                            AttendanceTrace = 1;
                    }

                }

            }

            //for each array index and if array index contains music, add this to total amount of music events
            for (int i = 0; i <= EventAttendance.Length; i++)
            {
                if (EventAttendance[i] > 0)
                {
                    if (iMusicAttendance > 0)
                        iMusicAttendance = iMusicAttendance + EventAttendance[i];
                    else
                        iMusicAttendance = EventAttendance[i];
                    }
            }
        }
    }

}

代码中有几个无用的变量,我冒昧地删除了它们。为了使用Linq,我还更改了
List
的数组

您在添加一个带有整行的
Convert.ToIn32
,因为
String.Remove()
不会更改调用它的对象,而是返回一个必须分配给某个对象的新字符串:line=line.Remove(0,18)

此外,您正在对计数器进行无用的检查:

if (MusicTrace != 0)
    MusicTrace = MusicTrace + 1;
else
    MusicTrace = 1;

MusicTrace++;
这导致我们:

if (!File.Exists(sVenueName.ToString() + ".txt"))
    return;

List<String> EventType = new List<string>();      //Store found event types
List<int> EventAttendance = new List<int>();      //Store Event Attendance

using (StreamReader file = new StreamReader(sVenueName + ".txt"))       //Using StreamReader
{
    while (!file.EndOfStream)
    {
        var line = file.ReadLine(); //Declare String to store line

        //Get All Music Event to Array
        if (line.Contains("Event Type: Music"))
        {
            EventType.Add("Music");        //[0] = Music
        }

        //Get All attendances to Array
        if (line.Contains("People Attending:"))
        {
            line = line.Remove(0, 18);
            EventAttendance.Add(Convert.ToInt32(line));   //[0] = 10
        }
    }
}

//for each array index and if array index contains music, add this to total amount of music events        
iMusicAttendance = EventAttendance.Sum();
如果(!File.Exists(sVenueName.ToString()+“.txt”))
返回;
List EventType=new List()//存储找到的事件类型
List EventAttention=新列表()//商店活动出席率
正在使用(StreamReader文件=新的StreamReader(sVenueName+“.txt”)//正在使用StreamReader
{
而(!file.EndOfStream)
{
var line=file.ReadLine();//声明要存储行的字符串
//将所有音乐事件设置为阵列
if(line.Contains(“事件类型:音乐”))
{
EventType.Add(“音乐”);//[0]=音乐
}
//让所有人都到阵列上去
if(第行包含(“参与人员:”)
{
行=行。删除(0,18);
eventAttention.Add(Convert.ToInt32(行));//[0]=10
}
}
}
//对于每个数组索引,如果数组索引包含音乐,则将其添加到音乐事件总数中
iMusicAttendance=eventAttention.Sum();
请更改:

while (!file.EndOfStream)
{
    line = file.ReadToEnd();

说明:

您一次读取整个文件,然后检查两个条件。但你要逐行阅读。因此,您需要使用
ReadLine

至于其余部分,您声明但从不使用
StreamReader RetrieveEvents
。你可以摆脱它

您可以使用
List
存储读取的信息。通过这种方式,您可以在代码中获得更大的灵活性。并且可以在没有循环的情况下计算

编辑:

我冒昧地把你的程序缩短了一点。下面的代码应该与您在文章中描述的完全一致:

string[] allLines = File.ReadAllLines(sVenueName + ".txt");
List<string> EventType = allLines.Where(x => x.Contains("Event Type: Music"))
                                 .Select(x => x = "Music").ToList();
List<int> EventAttendance = allLines.Where(x => x.Contains("People Attending:"))
                                    .Select(x => Convert.ToInt32(x.Remove(0,18))).ToList();

int iMusicAttendance = EventAttendance.Sum();

这将得到音乐人的总数;)希望它有帮助。

请将此减少到一个小范围。它不太可能是特定于Windows窗体的。。。还不清楚为什么要读取整个文件两次,一次是使用名为
line
的变量读取,该变量实际上是整个文件,因此可能有多行……在
iMusicAttendance
中得到了什么值?你期望得到什么?如果你的问题是出勤率。您应该写入line=line.Remove(0,18);此外,如果(MusicTrace!=0)MusicTrace=MusicTrace+1,则可以替换
;else MusicTrace=1
MusicTrace++
最后一个if语句,为什么在添加
eventAttention[i]
之前需要检查imustictEndance是否大于零?如果
eventAttention[i]>0
,您的意思是这样吗?我将代码更改为完全相同的值,但我无法将其发布到总值中。我将在按钮后添加文件内容和结果,供您在此处查看:@Lewis您可以将您测试的文本添加到您的问题中吗?
while (!file.EndOfStream)
{
    line = file.ReadLine();
string[] allLines = File.ReadAllLines(sVenueName + ".txt");
List<string> EventType = allLines.Where(x => x.Contains("Event Type: Music"))
                                 .Select(x => x = "Music").ToList();
List<int> EventAttendance = allLines.Where(x => x.Contains("People Attending:"))
                                    .Select(x => Convert.ToInt32(x.Remove(0,18))).ToList();

int iMusicAttendance = EventAttendance.Sum();
List<string> allLines = File.ReadAllLines("input.txt").ToList();
List<int> indices = Enumerable.Range(0, allLines.Count)
                              .Where(index => allLines[index].Contains("Event Type: Music"))
                              .Select(x => x+=3).ToList();

List<int> EventAttendance = allLines.Where(x => indices.Contains(allLines.IndexOf(x))).Select(x => Convert.ToInt32(x.Remove(0,18))).ToList();

int iMusicAttendance = EventAttendance.Sum();