C# 我知道当你转到下一个部分时,某些部分会起作用。谢谢。我试着从做类似事情的旧程序中提取两段代码,所以我希望它能起作用。显然,我有点不舒服。我现在就去修那东西。谢谢!更新了格式,希望它现在会更有用。我应该保留numberWins作为一种方法吗?我做了这些更改,
C# 我知道当你转到下一个部分时,某些部分会起作用。谢谢。我试着从做类似事情的旧程序中提取两段代码,所以我希望它能起作用。显然,我有点不舒服。我现在就去修那东西。谢谢!更新了格式,希望它现在会更有用。我应该保留numberWins作为一种方法吗?我做了这些更改,,c#,arrays,listbox,streamreader,C#,Arrays,Listbox,Streamreader,我知道当你转到下一个部分时,某些部分会起作用。谢谢。我试着从做类似事情的旧程序中提取两段代码,所以我希望它能起作用。显然,我有点不舒服。我现在就去修那东西。谢谢!更新了格式,希望它现在会更有用。我应该保留numberWins作为一种方法吗?我做了这些更改,但在上一个if语句中仍然存在相同的错误:/请看第四点,您正在尝试传递文件句柄而不是字符串数组。从头开始(尤其是在这样一个小应用程序中),一次构建小的、可编译的、正确的块,这绝对是一个很好的方法,并且是了解您正在做什么而不是试图修复其他人的错误的
我知道当你转到下一个部分时,某些部分会起作用。谢谢。我试着从做类似事情的旧程序中提取两段代码,所以我希望它能起作用。显然,我有点不舒服。我现在就去修那东西。谢谢!更新了格式,希望它现在会更有用。我应该保留numberWins作为一种方法吗?我做了这些更改,但在上一个if语句中仍然存在相同的错误:/请看第四点,您正在尝试传递文件句柄而不是字符串数组。从头开始(尤其是在这样一个小应用程序中),一次构建小的、可编译的、正确的块,这绝对是一个很好的方法,并且是了解您正在做什么而不是试图修复其他人的错误的好方法。
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!");
}
int numberWins(string teamName, string[] allChampions)
{
int numTitles = 0;
for (int i = 0; i < allChampions.Length; i++)
{
if (allChampions[i] == teamName)
numTitles++;
}
return numTitles;
}
while (index < championships.Length && !champFile.EndOfStream)
{
championships[index] = champFile.ReadLine();
index ++;
}