using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace 學生信息管理系統
{spa
class Program { static void Addstudent() //新建學生信息 { string ssudent; Console.WriteLine("請輸入學生的信息(學號、姓名、語文成績、數學成績、英語成績):"); FileStream fs = new FileStream("C:\\mystudent.txt", FileMode.Append, FileAccess.Write); StreamWriter sw = new StreamWriter(fs, Encoding.Default); ssudent = Console.ReadLine(); sw.WriteLine(ssudent); sw.Close(); } static void Searchstudent() { //查詢學生信息 StreamReader sw = new StreamReader("C:\\mystudent.txt"); string[] array = new string[] { }; //保存學生的各天信息 string stu; string number; Console.WriteLine("請輸入須要查詢的學生的學號:"); number = Console.ReadLine(); double allscore, avgscore,alls; allscore = avgscore = alls = Convert.ToDouble(0); stu = sw.ReadLine(); while (stu != null) { array = stu.Split(' '); allscore = Convert.ToDouble(array[2]) + Convert.ToDouble(array[3]) + Convert.ToDouble(array[4]); alls += allscore; if (array[0] == number||array[1]==number) { avgscore = allscore / 3; Console.WriteLine("學生的學號 姓名 語文成績 數學成績 英語成績各是:"); Console.WriteLine("{0} {1} {2} {3} {4}", array[0], array[1], array[2], array[3], array[4]); Console.WriteLine("總成績是:{0}\n平均成績是:{1}\n", allscore, avgscore); } stu = sw.ReadLine(); } Console.WriteLine("\n所有同窗的總成績是:{0}\n", alls); sw.Close(); } static void Main(string[] args) { do { Console.WriteLine("一、新建學生信息\n二、查詢學生信息\n三、退出系統\n"); int choice; Console.WriteLine("請輸入你的選擇:"); choice = Convert.ToInt32(Console.ReadLine()); switch (choice) { case 1: Addstudent(); break; case 2: Searchstudent(); break; case 3: Environment.Exit(0); break; } } while (true); } }
}code