题目是这样的,在多行文本中输入多行“姓名=成绩”格式的数据,要求输出成绩最高的学生的姓名和成绩
我设置了如下界面
textBox1是输入“成绩=分数”这种格式的,然后点击添加按钮(button1)显示到textBox2多行文本框里面,输入多行后,点击最高分的按钮(button2),然后最高分的姓名和成绩会以弹出框格式输出,大家看看代码,应该如何修改,才能使程序正常运行代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace TextBox练习3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
textBox2.AppendText(textBox1.Text+"\n");
textBox1.Text = "";
textBox1.Focus();
} private void button2_Click(object sender, EventArgs e)
{
string [] lines = textBox2.Lines;
string maxName = "";
int maxScore = -1;
foreach (string line in lines)
{
string [] strs=line.Split('=');
string name=strs[0];
string strScore=strs[1];
int score=Convert.ToInt32(strScore);
if (score >maxScore)
{
maxName=name;
maxScore=score;
}
} MessageBox.Show(string.Format("{0}是第一名,成绩为{1}",maxName,maxScore));
}
}
}
我设置了如下界面
textBox1是输入“成绩=分数”这种格式的,然后点击添加按钮(button1)显示到textBox2多行文本框里面,输入多行后,点击最高分的按钮(button2),然后最高分的姓名和成绩会以弹出框格式输出,大家看看代码,应该如何修改,才能使程序正常运行代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace TextBox练习3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
textBox2.AppendText(textBox1.Text+"\n");
textBox1.Text = "";
textBox1.Focus();
} private void button2_Click(object sender, EventArgs e)
{
string [] lines = textBox2.Lines;
string maxName = "";
int maxScore = -1;
foreach (string line in lines)
{
string [] strs=line.Split('=');
string name=strs[0];
string strScore=strs[1];
int score=Convert.ToInt32(strScore);
if (score >maxScore)
{
maxName=name;
maxScore=score;
}
} MessageBox.Show(string.Format("{0}是第一名,成绩为{1}",maxName,maxScore));
}
}
}
{
textBox2.AppendText(textBox1.Text + "\n");
textBox1.Text = "";
textBox1.Focus(); } private void button2_Click(object sender, EventArgs e)
{
string[] lines = textBox2.Lines;
string maxName = "";
int maxScore = -1;
foreach (string line in lines)
{
//line会出现空值所以会出错
if (line.Trim().Length > 0)
{
string[] strs = line.Split('=');
string name = strs[0];
string strScore = strs[1];
int score = Convert.ToInt32(strScore);
if (score > maxScore)
{
maxName = name;
maxScore = score;
}
}
} MessageBox.Show(string.Format("{0}是第一名,成绩为{1}", maxName, maxScore)); }
在这段代码中 private void button1_Click(object sender, EventArgs e)
{
textBox2.AppendText(textBox1.Text+"\n");
textBox1.Text = "";
textBox1.Focus();
}textBox2.AppendText(textBox1.Text+"\n");会使你每次添加一个新行后又进行了一次换行,这样在你使用string[] lines = textBox2.Lines时,会多读出一行空行。
修改方法,可以使用5l的方法。干脆多读出的一行忽略掉。
也可以从根本上改变,就是首次添加直接写文本,以后在加新行前加个回车换行。
使用以下的代码替换上面的代码: private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text == "") textBox2.Text = textBox1.Text;
else textBox2.AppendText("\r\n"+textBox1.Text);
textBox1.Text = "";
textBox1.Focus();
}