문제

I'm sure I'm making this much harder than it really is, but my brain hurts....

I have a program that reads a list of baseball teams from a .txt file (called Teams) and displays them into a listbox. I have another .txt file (called WorldSeries) that displays, in order, which teams have won the World Series. The WorldSeries file looks like this (minus the bullet points):

  • Boston Americans
  • New York Giants
  • Chicago White Sox
  • Chicago Cubs
  • Chicago Cubs
  • Pittsburgh Pirates
  • ...

Meaning The Boston Americans won the first World Series, Giants second, White Sox third, etc. The dates range from 1903-2009 accordingly, however there was no World Series in 1904 or 1994.

The program allows you to select a team from the list box, click a button, and a MessageBox tells you how many times that team has won the World Series. I thought it sounded pretty easy, but I'm having a harder time than I thought I would. I think I'm doing it way wrong. Can someone please help. Thanks!

    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!");
            }
도움이 되었습니까?

해결책

Several problems with your code:

  1. The statement if (numberWins(champFile, selectedTeam) != 1) is going to fail if a team won 2, 3, 4, etc. world series. This should be >= 1.

  2. The "numberWins" function takes a list of integers, but you are calling it with two arguments, which will fail compilation.

  3. The "numberWins" function itself doesn't make any sense, you are accumulating every value in the array, not checking for a team name at all. It should look like:

    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. The while loop to read the file doesn't make any sense, you are reading a list of team names into an integer array! Make championships a string array, and change your loop to:

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

then call numberWins as numberWins(selectedTeam, championships);

Fixing those should get you closer. I will edit if I notice anything else.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top