请问一下,C#中如何才能随机显示自己存在TXT文档中的单词?
还有用 StreamReader sr=new StreamReader("example.txt",System.Text.Encoding.GetEncoding("gb2312"));
{
String line;
while ((line = sr.ReadLine()) != null)
{
i += 1;
english = sr.ReadLine();
chinese = sr.ReadLine();
}
为什么出来的单词都是一样的? 谢谢
还有用 StreamReader sr=new StreamReader("example.txt",System.Text.Encoding.GetEncoding("gb2312"));
{
String line;
while ((line = sr.ReadLine()) != null)
{
i += 1;
english = sr.ReadLine();
chinese = sr.ReadLine();
}
为什么出来的单词都是一样的? 谢谢
解决方案 »
- 编了个程序报错无法将类型“int”隐式转换为“System.Windows.Thickness”
- vs2005和vista的问题
- 初试正则表达式,遇到点问题,特来请教...
- 如何将datatable里的两列的数据导入到另一个datatable的指定两列
- vss救命
- asp里的rs.eof属性在ado.net里有嘛?
- 类型转换难题,怎样根据控件类型名称转换到对应的控件
- 在C#中怎么设置treeview得节点为checkbox类型???
- 紧急 winservice 如何获取 当前系统应用程序列表
- 请问代理能不能跨进程使用啊?比如在Remoting或者com+里面使用?
- 急求!MSchart怎么用?
- 新手请教,请帮忙看看我写的代码,一点击按钮就显示缺少对象
你想随机读啊,那就在while ((line = sr.ReadLine()) != null))里面随机break吧
我是在最后个括号外面写了句
label1.Text = english[i];
因为我的TXT文件中是一行中文 一行英文,但是我想让他只出现英文
可我这样写每次出来的都是中文而且不会变
读取一行的字符啊
你的循环就是一直读到尾,你在循环中加个msgbox就能看到每行的数据了。
ArryayList al=new ArrayList();
while ((line = sr.ReadLine()) != null)
{
al.Add(sr.ReadLine());
}
//随即显示 Random r=new Random();
int i=r.Next();
this.TextBox.text=al[i].ToString();
我做的是单击一下按钮单词就换一个,要从第一个到最后一个,我用的是label.text
ArryayList al=new ArrayList();
while (sr.ReadLine() != null)
{
al.Add(sr.ReadLine());
}
//随即显示
Random r=new Random();
int i=r.Next();
this.TextBox.text=al[i].ToString();
这个是什么意思?
ArryayList al=new ArrayList();
al是个集合,就是动态的数组
{
String line;
Random rnd = new Random();
while ((line = sr.ReadLine()) != null)
{
i += 1;
english = sr.ReadLine();
chinese = sr.ReadLine(); int i = rnd.Next(0, 20);
if (i == 0)
{
break;
}
}
}
//定义成全局 变量
ArryayList al=new ArrayList();
int i=0;
//加载事件
private void Form1_Load(object sender, EventArgs e)
{
StreamReader sr = new StreamReader("example.txt", System.Text.Encoding.GetEncoding("gb2312")); while (sr.ReadLine() != null)
{
al.Add(sr.ReadLine());
}
}private void button1_Click(object sender, EventArgs e)
{
if (al.Count ==0)
return;
if(i>=al.Count)
return;
label1.Text = al[i].ToString();
i++;
}
ArryayList al=new ArrayList();
int i=0;
//加载事件
private void Form1_Load(object sender, EventArgs e)
{
StreamReader sr = new StreamReader("example.txt", System.Text.Encoding.GetEncoding("gb2312")); while (sr.ReadLine() != null)
{
al.Add(sr.ReadLine());
}
}private void button1_Click(object sender, EventArgs e)
{
if (al.Count ==0)
return;
if(i>=al.Count)
{
i=0;//重新开始循环
return;
}
label1.Text = al[i].ToString();
i++;
}
为什么随机不是每次都成功的
参数名: index
一直出来这个
ArryayList al=new ArrayList();
while (sr.ReadLine() != null)
{
al.Add(sr.ReadLine());
}
//随即显示
Random r=new Random();
int i=r.Next();
this.TextBox.text=al[i%al.Length].ToString();
不好意思
忘记了,this.TextBox.text=al[i%al.Length].ToString();