黑马程序员技术交流社区
标题:
Winform 统计成绩问题
[打印本页]
作者:
王超洋
时间:
2012-5-18 15:50
标题:
Winform 统计成绩问题
本帖最后由 王超洋 于 2012-5-18 15:55 编辑
private void button1_Click(object sender, EventArgs e)
{
string[] lines = textBox1.Lines;
string maxname = "";
int maxscore = 0;
foreach(string line in lines)
{
string[] str=line.Split('=');
string name = str[0];
string strscore = str[1];
int score = Convert.ToInt32(strscore);
if(score>maxscore)
{
maxname = name;
maxscore = score;
}
}
MessageBox.Show(string.Format("{0}是第一名,成绩为{1}", maxname, maxscore));
return;
}
这个有点看不懂,让统计最高成绩的,foreach(string line in lines)这个是什么循环?
解释一下逻辑?
作者:
郑玉赛
时间:
2012-5-18 16:32
string[] lines = textBox1.Lines;//将textbox1每一行的值都当做lines数组里的一个元素
string maxname = "";
int maxscore = 0;
foreach (string line in lines)//这句是遍历每一行内容
{
string[] str = line.Split('=');//字符串分割,分割之后是以个str数组。
string name = str[0];//name为数组里的第一个元素
string strscore = str[1];//strscore为数组里的第二个元素
int score = Convert.ToInt32(strscore);
if (score > maxscore)//这里就是比较大小了。
{
maxname = name;
maxscore = score;
}
}
MessageBox.Show(string.Format("{0}是第一名,成绩为{1}", maxname, maxscore));
return;
foreach(string line in lines)这个就是遍历用的循环
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2