using System.Collections;
using System;
using System.Text;
String History = "2";
String ZhengZhi = "1";
String Way1 = "1";
String Way2 = "2";
Console.WriteLine("选择背诵科目;政治请输入1,历史输入2");
String Subject = Console.ReadLine();
Console.WriteLine("选择背诵方式;顺序请输入1,乱序输入2");
String Way = Console.ReadLine();
if (Subject== History && Way == Way1)
{
int counterHistory = 0;
foreach (string line in System.IO.File.ReadLines(@".\History.txt"))
{
System.Console.WriteLine(line);
counterHistory++;
System.Console.ReadKey();
}
System.Console.WriteLine("There were {0} lines.", counterHistory);
System.Console.ReadLine();
Console.ReadKey();
}
if (Subject == History && Way == Way2)
{
StreamReader streamReader = new(@".\History.txt", Encoding.UTF8); //读取指定文本文档
ArrayList arrayList = new ArrayList();//建立动态数组
Random random = new Random();//随机数
while (streamReader.Peek() >= 0) //逐行读取文本文档
{
arrayList.Add(streamReader.ReadLine());//将读取到的行写到数组中
}
for (int a = 0; ; a++)
{
int i = random.Next(0, 104); //定义随机数的范围(a,b)b为txt文件最大行数
string str = arrayList[i].ToString(); //将读取到的随机数组值赋给str
Console.WriteLine(str); //打印出str
Console.ReadKey();
}
}
if (Subject == ZhengZhi && Way== Way1)
{
int counterZhengZhi = 0;
foreach (string line in System.IO.File.ReadLines(@".\ZhengZhi.txt"))
{
System.Console.WriteLine(line);
counterZhengZhi++;
System.Console.ReadKey();
}
System.Console.WriteLine("There were {0} lines.", counterZhengZhi);
System.Console.ReadLine();
Console.ReadKey();
}
if (Subject == ZhengZhi && Way == Way2)
{
StreamReader streamReader = new(@".\ZhengZhi.txt", Encoding.UTF8); //读取指定文本文档
ArrayList arrayList = new ArrayList();//建立动态数组
Random random = new Random();//随机数
while (streamReader.Peek() >= 0) //逐行读取文本文档
{
arrayList.Add(streamReader.ReadLine());//将读取到的行写到数组中
}
for (int a = 0; ; a++)
{
int i = random.Next(0, 104); //定义随机数的范围(a,b)b为txt文件最大行数
string str = arrayList[i].ToString(); //将读取到的随机数组值赋给str
Console.WriteLine(str); //打印出str
Console.ReadKey();
}
}
else
{
Console.WriteLine("你想要的模式还没开发呀OvO");
}
下面这一段代码借鉴了weixin_39913807的文章c# 逐行写txt_C#从txt文本随机读取一行的解决方案-网络教程与技术-亦是美网络..._weixin_39913807的博客-CSDN博客
StreamReader streamReader = new(@".\ZhengZhi.txt", Encoding.UTF8); //读取指定文本文档
ArrayList arrayList = new ArrayList();//建立动态数组
Random random = new Random();//随机数
while (streamReader.Peek() >= 0) //逐行读取文本文档
{
arrayList.Add(streamReader.ReadLine());//将读取到的行写到数组中
}
for (int a = 0; ; a++)
{
int i = random.Next(0, 104); //定义随机数的范围(a,b)b为txt文件最大行数
string str = arrayList[i].ToString(); //将读取到的随机数组值赋给str
Console.WriteLine(str); //打印出str
Console.ReadKey();
}
在此致以诚挚的感谢