我确信我比实际做得更难,但是我的大脑很疼......
我有一个程序,它从.txt文件(称为团队)中读取棒球队列表,并将它们显示在列表框中。我有另一个.txt文件(称为WorldSeries),按顺序显示哪些球队赢得了世界大赛。 WorldSeries文件看起来像这样(减去要点):
意义波士顿美国人赢得了第一个世界系列赛,巨人队获得第二,白袜队获得第三,等等。日期从1903年至2009年相应,但1904年或1994年没有世界大赛。
该程序允许您从列表框中选择一个团队,单击一个按钮,MessageBox会告诉您该团队赢得世界系列赛的次数。我觉得这听起来很简单,但我比我想象的要难得多。我想我做错了。有人可以请帮助。谢谢!
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!");
}
答案 0 :(得分:0)
您的代码有几个问题:
如果一支球队赢得了2,3,4等世界系列赛,那么if (numberWins(champFile, selectedTeam) != 1)
声明就会失败。这应该是&gt; = 1。
“numberWins”函数采用整数列表,但是您使用两个参数调用它,这将使编译失败。
“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;
}
读取文件的while循环没有任何意义,您正在将团队名称列表读入整数数组!使锦标赛成为一个字符串数组,并将你的循环改为:
while (index < championships.Length && !champFile.EndOfStream)
{
championships[index] = champFile.ReadLine();
index ++;
}
然后将numberWins称为numberWins(selectedTeam, championships);
修复这些应该让你更接近。如果我注意到其他任何事情,我会编辑。