c#小问题请高手教教我啊

w5057723 2010-12-26 03:27:19
string[] juzi = { "Last week I went to the theatre. I had avery good seat.", "I looked at the man and the woman angrily.",
"They were talking loudly. I got very angry.", "It was Sunday. I never get up early on Sundays.",
"Last Sunday I got up very late.","I am only interested in sitting in a boat and doing nothing at all !",
"Aeroplanes are slowly driving me mad.", "I am one of the few people left.", "We have an old musical instrument.",
"It is being repaired by a friend of my father's.","Ten minutes later we walked out of the shop together."
};

谁能帮我分割这些字符串 不必全分了 就告诉我 怎么分割
简单的说明下我的目的: 我需要做一类似金山打字通的东西
现在介绍下我的程序: 我的程序会随机输出上面的句子的其中只几个 现在要求用户按照程序显示的字符串进行输入 然后程序会输出正确几个 错几个 。 现在这些句子我的程序在判断时一但用户输入错一个字母 我的程序就会显示整个句子错误 ,这样就达不到我的要求 不能输出 用户正确几个错误几个 。
所以我需要:我的程序可以一个一个字母的判断。
我记得老师说过一个方法 可以把字符串分割成一个个字符那种方法
麻烦 高手教教我啊 谢谢
...全文
108 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gary9529 2010-12-28
  • 打赏
  • 举报
回复
记得将文本框改成只读

richTextBox1.ReadOnly = true;
nhc190 2010-12-28
  • 打赏
  • 举报
回复
取出字符串相同部分
var c=str[0].ToCharArray().Intersect(TextBox1.Text.ToCharArray()).ToArray();
Gary9529 2010-12-27
  • 打赏
  • 举报
回复
简单注释了一下。你看下



public partial class Form1 : Form
{
// 字符串数组
string[] juzi = { "Last week I went to the theatre. I had avery good seat.", "I looked at the man and the woman angrily.",
"They were talking loudly. I got very angry.", "It was Sunday. I never get up early on Sundays.",
"Last Sunday I got up very late.","I am only interested in sitting in a boat and doing nothing at all !",
"Aeroplanes are slowly driving me mad.", "I am one of the few people left.", "We have an old musical instrument.",
"It is being repaired by a friend of my father's.","Ten minutes later we walked out of the shop together."
};


char[] chats = null;// 声明一个字符数组

public Form1()
{InitializeComponent();// 忽略

// 在这下面,主窗体的构造函数中初始化要用的变量

// 设置文本框中的文本 juzi 是个字符串数组 ,这里只取第一个下标的字符串
// 如果想用 juzi 字符串数组里的其他文本,改[]下标
this.richTextBox1.Text = juzi[0];

// richTextBox1.Text 是文本框中的字符串,调用 ToCharArray() 方法会将这串字符转换成一个 字符数组,
// 返回的字符数组赋给前面声明的 chats 字符数组
chats = richTextBox1.Text.ToCharArray();
this.richTextBox1.SelectionStart = 0;// 这里设置文本框光标的选定起始点
this.richTextBox1.SelectionLength = 1;// 这里设置文本框光标要选定几个字符
this.richTextBox1.SelectionColor = Color.Blue;// 这里设置文本框光标选定字符的颜色为蓝色
}


private void Form1_Load(object sender, EventArgs e)
{
// 文本框的事件委托(在文本有输入焦点时,按下键时发生) ,事件发生时 让它调用下面的richTextBox1_KeyPress()方法
this.richTextBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.richTextBox1_KeyPress);
}



// 键盘按下并释放后的事件调用方法
// 当在键盘上按下,并弹起后会执行这个方法
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// [选定字符起始位置]。
// chats 字符数组[]中写个下标,
// richTextBox1.SelectionStart也就是当前文本框的选定的第几个字符
// 用参数 e.KeyChar 属性得到键盘按下的哪个字符
// 并与 chats[] 里的字符,进行比较
if (chats[richTextBox1.SelectionStart] == e.KeyChar)
{
// 如果上面 if 判断为 true ,
// 表示正确,这里面写其他,正确时要做的事,如:正确数加1
}
else
{ // 如果上面 if 判断为 false ,
// 错误 设置文本框光标选定字符的颜色为红色
richTextBox1.SelectionColor = Color.Red;

// 错误数加1
}
richTextBox1.SelectionStart++;// 让光标加一,
richTextBox1.SelectionColor = Color.Blue;// 设置文本框光标选定字符的颜色为蓝色

}
Gary9529 2010-12-26
  • 打赏
  • 举报
回复
char[] chars = juzi[0].ToCharArray();
然后取 chars[0] 里的字符

================================

public partial class Form1 : Form
{
char[] chats = null;
public Form1()
{
InitializeComponent();
this.richTextBox1.Text = "Last week I went to the theatre. I had avery good seat. I looked at the man and the woman angrily.";
chats = richTextBox1.Text.ToCharArray();
this.richTextBox1.SelectionStart = 0;
this.richTextBox1.SelectionLength = 1;
this.richTextBox1.SelectionColor = Color.Blue;
}

private void Form1_Load(object sender, EventArgs e)
{
}




// 键盘按下并释放后的事件调用方法
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// [选定字符起始位置]
if (chats[richTextBox1.SelectionStart] == e.KeyChar)
{
// 正确
}
else
{
// 错误
richTextBox1.SelectionColor = Color.Red;
}
richTextBox1.SelectionStart++;
richTextBox1.SelectionColor = Color.Blue;

}
}
  • 打赏
  • 举报
回复
strng.ToCharArray()
wuyq11 2010-12-26
  • 打赏
  • 举报
回复
取出字符串相同部分
var c=str[0].ToCharArray().Intersect(TextBox1.Text.ToCharArray()).ToArray();
netharry 2010-12-26
  • 打赏
  • 举报
回复
记得有 string.indexof

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧