导航

黑马程序员----一个小小练习

Posted on 2011-11-30 18:06  asp.net 开发  阅读(132)  评论(0)    收藏  举报

今天做了一个练习:是输出最大成绩的,用的是split方法实现分割字符串的,具体代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}

private void btnOk_Click(object sender, EventArgs e)
{
string Content = txtContent.Text;
string maxName="";
int maxScore=-1;
string[] lines = Content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
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));
string str1 = "123";
MessageBox.Show(str1.Substring(1,1));
}

private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(listBox1.Text))
{
MessageBox.Show("没有内容");
}
else
{
string str = listBox1.Text.Substring(1, 2);
MessageBox.Show(str);
}
}
}
}

 

在写的过程中暴漏的问题很多,比如在运行是总是报一个“使用了未赋值的局部变量maxName”弄了半天才发现maxName没有赋初值,这程序是看着容易,一写就不是那么回事,还是练的少啊,今后还要多加练习。