从.txt文件中计算项目并显示结果- c#

本文关键字:显示 结果 项目 txt 文件 计算 | 更新日期: 2023-09-27 17:49:36

我知道我把事情弄得比实际更困难了,但是我的脑子好痛....

我有一个程序,它从一个.txt文件(称为teams)中读取棒球队列表,并将它们显示到一个列表框中。我还有另一个.txt文件(称为WorldSeries),它按顺序显示哪些球队赢得了世界大赛。WorldSeries文件看起来像这样(去掉项目符号):

    美国波士顿
  • 纽约巨人队
  • 芝加哥白袜队
  • 芝加哥小熊队
  • 芝加哥小熊队

意思是波士顿美国人赢得了第一次世界大赛,巨人队第二,白袜队第三,等等。日期从1903年到2009年不等,但1904年和1994年没有世界职业棒球大赛。

该程序允许您从列表框中选择一个球队,单击一个按钮,然后一个消息框告诉您该球队赢得了多少次世界大赛。我以为这听起来很简单,但我现在的处境比我想象的要困难。我觉得我做错了。有人能帮帮我吗?谢谢!

    private int numberWins(int[] iArray) // Method to count total number of wins
    {
        int totalWins = 0; // Accumulator, intitialized to 0.
        // Step through the array, adding each element to the Accumulator
        for (int index = 0; index < iArray.Length; index++)
        {
            totalWins += iArray[index];
        }
        // Return the number of wins
        return numberWins;
    }
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
    private void worldSeriesApp_Load(object sender, EventArgs e)
    {
        try
        {
            string teamName; // To hold the teams name
            StreamReader inputTeams; // For file input
            inputTeams = File.OpenText("Teams.txt"); // Open the file for  team list
            teamList.Items.Clear(); // Clear all items currently in the List Box
            while (!inputTeams.EndOfStream) // While not at the end of the list of teams
            {
                teamName = inputTeams.ReadLine(); // Read each line
                teamList.Items.Add(teamName); // Add each line to the List Box
            }
            inputTeams.Close(); // Close the file
        }
        catch (Exception ex) // Catch any errors
        {
            MessageBox.Show(ex.Message); // Let the user know there is an error
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        // Local variables
        const int SEASONS = 104; // Number of Seasons between 1903 and 2009
        int[] championships = new int [SEASONS]; // Array of seasons
        StreamReader champFile; // For file input
        int index = 0; // Loop counter
        string selectedTeam; // Team selected from the List Box

        // Open the file and get StreamReader object
        champFile = File.OpenText("WorldSeries.txt");
        //Read the files contents into the array
        while (index < championships.Length && !champFile.EndOfStream)
        {
            championships[index] = int.Parse(champFile.ReadLine());
            index ++;
        }
        if (teamList.SelectedIndex !=-1) // If a team is selected
        {
            //Get the selected item.
            selectedTeam = teamList.SelectedItem.ToString();
            // Determine if the team is in the array.
            if (numberWins(champFile, selectedTeam) != 1) // If so...
            {
                // Display how many times that team has won the World Series
                MessageBox.Show("The " + selectedTeam + "has won the World Series"
                numberWins + "times!");
            }
            else // Otherwise..
            {
                // Let them know their team sucks.
                MessageBox.Show("The " + selectedTeam + "sucks because they have never
                won the World Series!");
            }

从.txt文件中计算项目并显示结果- c#

代码中的几个问题:

  1. 如果一支球队赢得了2、3、4等次世界大赛,声明if (numberWins(champFile, selectedTeam) != 1)将失败。

  2. "numberWins"函数接受一个整数列表,但是你用两个参数调用它,这将导致编译失败。

  3. "numberWins"函数本身没有任何意义,你正在累积数组中的每个值,根本不检查团队名称。它应该看起来像:

    int numberWins(string teamName, string[] allChampions)
    {
       int numTitles = 0;
       for (int i = 0; i < allChampions.Length; i++)
       {
          if (allChampions[i] == teamName)
            numTitles++;
        }
        return numTitles;
     }
    
  4. 读取文件的while循环没有任何意义,你正在将一个团队名称列表读取到一个整数数组中!将championships设置为字符串数组,并将循环设置为:

    while (index < championships.Length && !champFile.EndOfStream)
    {
        championships[index] = champFile.ReadLine();
        index ++;
    }
    

then call numberWins as numberWins(selectedTeam, championships);

修复这些应该会让你更接近。