求解决,文件读取问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace stu
{
class Stu
{
public string info;
public string name;
public int age;
public string sex;
public int num;
public double MathScore;
public double EnglishScore;
public double ChineseScore;
public void InputScore()
{
Console.WriteLine("输入数学成绩:");
this.MathScore = double.Parse(Console.ReadLine());
Console.WriteLine("输入英语成绩:");
this.EnglishScore = double.Parse(Console.ReadLine());
Console.WriteLine("输入语文成绩:");
this.ChineseScore = double.Parse(Console.ReadLine());
info += "语文:" + ChineseScore.ToString() + Environment.NewLine + "数学:" + MathScore.ToString() + Environment.NewLine+ "英语:" + EnglishScore.ToString()+Environment.NewLine+ "总分:" + SumScore().ToString() + Environment.NewLine;
}
public double SumScore()
{
double Sum = 0;
Sum = this.ChineseScore + this.EnglishScore + this.MathScore;
return Sum;
}
public void ShowScore()
{
Console.WriteLine("语文成绩:{0}", this.ChineseScore);
Console.WriteLine("数学成绩:{0}", this.MathScore);
Console.WriteLine("英语成绩:{0}", this.EnglishScore);
Console.WriteLine("总成绩:{0} ", this.SumScore());
}
public Stu()
{
Console.WriteLine("请输入 (姓名 年龄 性别 学号)");
this.name = Console.ReadLine();
this.age = int.Parse(Console.ReadLine());
this.sex = Console.ReadLine();
this.num = int.Parse(Console.ReadLine());
info +="姓名:"+ name + Environment.NewLine +"年龄:" +age.ToString() + Environment.NewLine + "性别:"+sex + Environment.NewLine + "学号:"+num.ToString() + Environment.NewLine;
}
public Stu(int i)
{
this.age = i;
}
public void show()
{
Console.WriteLine("姓名:{0}", this.name);
Console.WriteLine("年龄:{0}", this.age);
Console.WriteLine("性别:{0}", this.sex);
Console.WriteLine("学号:{0}", this.num);
}
public void change()
{
Console.WriteLine("请输入 (姓名 年龄 性别 学号)");
this.name = Console.ReadLine();
this.age = int.Parse(Console.ReadLine());
this.sex = Console.ReadLine();
this.num = int.Parse(Console.ReadLine());
}
}
class Program
{
public static string filePath = (@"C:\Users\ra0mb1er\Desktop\c#\1.txt");//定义全局变量文件路径
public static string str;
public static string temp;
int index;
Stu[] s = new Stu[100]; //定义100个学生数组
public void Creat() //创建班级学生信息
{
string c;
for (int i = 0; i < s.Length; i++)
{
Console.WriteLine("正在创建学生信息 Ing。。。。");
index = i + 1;
s[i] = new Stu();
Console.WriteLine("继续输入(y/n)");
c = Console.ReadLine().ToLower();
if (c == "n")
break;
}
}
public void Search()
{
int i;
long xh01;
string name01;
string str;
Console.WriteLine("查找姓名,或学号");
Console.WriteLine("请选择查询方式,1代表学号查询,2代表姓名查询");
i = int.Parse(Console.ReadLine());
switch (i)
{
case 1:
Console.WriteLine("请输入要查询的学号");
xh01 = int.Parse(Console.ReadLine());
for (int d = 0; d < index; d++)
{
if (s[d].num == xh01)
{
s[d].show();
Console.WriteLine("是否要删除学生信息(y/n)");
str = Console.ReadLine().ToLower();
if (str == "y")
{
if (d == index - 1)
index--;
else
{
index--;
for (; d < index; d++)
{
s[d] = s[d + 1];
}
}
}
Console.WriteLine("是否要修改学生信息(y/n)");
str = Console.ReadLine().ToLower();
if (str == "y") s[d].change();
break;
}
}
break;
case 2:
Console.WriteLine("请输入要找到的姓名");
name01 = Console.ReadLine();
for (int d = 0; d < index; d++)
{
if (s[d].name == name01)
{
Console.WriteLine("是否要修改学生信息");
str = Console.ReadLine().ToLower();
if (str == "y") s[d].change();
s[d].show();
}
}
break;
}
}
public void ScoreInput()
{
for (int i = 0; i < index; i++)
{
Console.WriteLine("{0}学生成绩:",s[i].name);
s[i].InputScore();
str += s[i].info + Environment.NewLine;
}
}
public void sort()
{
int a;
Console.WriteLine("请选择排序方式,1代表学号排序,2代表总成绩排序");
a = int.Parse(Console.ReadLine());
switch (a)
{
case 1:
for (int i = 0; i < index; i++)
{
for (int j = 0; j < index; j++)
{
if (s[j].num < s[i].num)
{
Stu st01;
st01 = s[j];
s[j] = s[i];
s[i] = st01;
}
}
}
break;
case 2:
for (int i = 0; i < index; i++)
{
for (int j = 0; j < index; j++)
{
if (s[j].num < s[i].num)
{
Stu st01;
st01 = s[j];
s[j] = s[i];
s[i] = st01;
}
}
}
break;
}
}
public void add()
{
s[index] = new Stu();
index++;
}
public void Display()
{
for (int i = 0; i < index; i++)
{
s[i].show();
s[i].ShowScore();
}
}
public void read()
{
StreamReader fs = File.OpenText(filePath);
while ((temp = fs.ReadLine()) != null)
{
Console.WriteLine(temp);
}
fs.Close();
}
public void write(string str)
{
using (StreamWriter SW = File.AppendText(filePath))//using合理有效释放资源
{
SW.WriteLine(str);
SW.Close();
}
}
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine("已经存在的学生信息");
p.read();
p.Creat();
p.ScoreInput();
p.Display();
p.write(str);
Console.WriteLine("写入完毕!");
Console.WriteLine("是否搜索学生信息(y/n)");
string str01= Console.ReadLine().ToLower();
if (str01 == "y")
{
p.read();
p.Search();
}
}
}
}
read()函数在读取时,如何把读入的信息重新赋值到 Stu方法中定义的this.age,this.name变量中, 读入的文件内容如下
姓名:admin
年龄:10
性别:man
学号:110
语文:100
数学:100
英语:100
-------------------------------------
求解决,求高手 c# --------------------编程问答-------------------- 根据读到的信息进行字符串和类型转换的操作
你用xml或者数据库都比较好过文本文件 --------------------编程问答-------------------- 每一行前面不是都有说明是哪一科吗,解析出后面的数字,直接写到结构体对应的属性就行了,不过你这种存储只能一条条写if语句去赋值,你应该用流结合struct整体读入 --------------------编程问答-------------------- 小弟 新手比较菜唉 --------------------编程问答-------------------- 小弟新手比较菜唉,我就想修改一个read()函数 --------------------编程问答-------------------- 目前还没有学数据库
补充:.NET技术 , C#