变量不添加其他变量值

时间:2017-03-24 12:20:00

标签: c# winforms streamreader

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

如果文件中的line包含"Event Type: Music",请使用"Music"EventType[]存储在MusicTrace数组中。音乐跟踪从0开始,每次找到上面的字符串时都会递增。所以它在数组中运行。数组大小是文件中的行数,以确保始终有足够的数组空间。

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

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

来计算每个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];
                    }
            }
        }
    }

}

然后点击按钮显示出席情况:

private void btnShow_Click(object sender, EventArgs e)
    {
        lblMusicOutput.Text = "After analysis, we can see that Music Events have a total attendance of " + iMusicAttendance;
        lblArtOutput.Text = "After Analysis, we can see that Events have a total Attenance of " + iArtAttendance;
        lblDance.Text = "After Analysis, we can see that Dance Events have a total Attenance of " + iDanceAttendance;
        lblTheatreOutput.Text = "After Analysis, we can see that Theatre Events have a total Attenance of " + iTheatreAttendance;
    }

2 个答案:

答案 0 :(得分:0)

在你的代码里面有几个无用的变量,我冒昧地删除了。我还更改了List<T>的数组以使用Linq。

您添加了一个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();

答案 1 :(得分:0)

请更改:

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

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

说明:

您正在阅读整个文件,然后检查一下您的两个条件。但是你想逐行阅读。所以你需要使用ReadLine

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

您可以使用List<T>存储读取的信息。这样,您可以更灵活地进入代码。并且可以在没有循环的情况下计算Sum

修改

我冒昧地减少了你的程序。下面的代码应该完全按照您在帖子中描述的内容执行:

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();

EDIT2:

看到你的文件内容很明显,你只想总结出席音乐活动的人,但在你的方法中,你总结所有参加所有活动的人。

查看您的文件,您似乎有3行的偏移量。所以我建议,获取Music行的所有索引,然后只获取3行的数字:

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();

这将为您提供只有音乐人的总和;)跳跃它有帮助。