1
class Program
{
    static string strFile = "Student Database.txt";

    static void Main(string[] args)
    {

        string strInput = null; // user input string

    start:
        System.IO.DirectoryInfo dir = new DirectoryInfo("student_results.txt");

        // Request user input as to actions to be carried out
        Console.WriteLine("\nWhat do you want to do?\n" +
            " 1.View Student(s)\n 2.Add a New Student\n 3.Exit program");
        // Save user input to make decision on program operation
        strInput = Console.ReadLine();

        // Switch statement checking the saved user input to decide the action
        // to be carried out
        switch (strInput)
        {
            case "1": // choice for view file
                Console.Clear();

                string file = AppDomain.CurrentDomain.BaseDirectory +
                    @"student_results.txt";
                StreamReader sr = new StreamReader(file);
                string wholeFile = sr.ReadToEnd();
                Console.Write(wholeFile + "");
                sr.Close();

                goto start;
            ...
        }
        ...
    }
    ...
}

我希望我的这部分代码只是单独阅读学生并将他们转发给我,而不是当我按下“1)查看学生”时它只是将他们全部回叫给我几乎说“请输入您要查看的学生的学生姓名或身份证号”。我目前有一个随机数生成器运行的 ID 号。

谢谢你们的时间。

4

3 回答 3

1

欢迎使用 SO,首先goto在 99% 的情况下在 C# 中不是一个好的选择,您最好使用循环。对于您的代码,我会将每个学生保存在一行中,并且在阅读学生时,我会逐行阅读它们,直到找到该学生。

class Program
{
    static string strFile = "Student Database.txt";
    static void Main(string[] args)
    {
        string strInput = ""; // user input string

        while (strInput != "3")
        {
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("student_results.txt");
            Console.WriteLine("\nWhat do you want to do?\n 1.View Student(s)\n 2.Add a New Student\n 3.Exit program"); // request user input as to actions to be carried out
            strInput = Console.ReadLine(); //save user input to make decision on program operation

            switch (strInput)
            {
                case "1":
                    Console.Clear();
                    Console.WriteLine("Enter Student ID: \n");
                    string file = AppDomain.CurrentDomain.BaseDirectory + @"student_results.txt";
                    StreamReader sr = new StreamReader(file);
                    string StudentID = Console.ReadLine();
                    string line = "";
                    bool found = false;
                    while((line = sr.ReadLine()) != null)
                    {
                        if (line.Split(',')[0] == StudentID)
                        {
                            found = true;
                            Console.WriteLine(line);
                            break;
                        }
                    }
                    sr.Close();
                    if (!found)
                    {
                        Console.WriteLine("Not Found");
                    }
                    Console.WriteLine("Press a key to continue...");
                    Console.ReadLine();
                    break;
                case "2":
                    Console.WriteLine("Enter Student ID : ");
                    string SID = Console.ReadLine();
                    Console.WriteLine("Enter Student Name : ");
                    string SName = Console.ReadLine();
                    Console.WriteLine("Enter Student Average : ");
                    string average = Console.ReadLine();
                    string wLine = SID + "," +SName+":"+average;
                    file = AppDomain.CurrentDomain.BaseDirectory + @"student_results.txt";
                    StreamWriter sw = File.Exists(file) ? File.AppendText(file) : new StreamWriter(file);
                    sw.WriteLine(wLine);
                    sw.Close();
                    Console.WriteLine("Student saved on file, press a key to continue ...");
                    Console.ReadLine();
                    Console.Clear();
                    break;
                case "3":
                    return;
                default:
                    Console.Clear();
                    Console.WriteLine("Invalid Command!\n");
                    break;
            }
        }
    }
}

这段代码可能不完整,我想给你一个想法,希望对你有帮助。

于 2012-12-10T20:31:09.787 回答
0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ReadStudents
{
    class Program
    {
        static string _filename = "students.txt";

        static void Main(string[] args)
        {
            List<Student> students = new List<Student>();

            // Load students.
            StreamReader reader = new StreamReader(_filename);
            while (!reader.EndOfStream)
                students.Add( new Student( reader.ReadLine()));
            reader.Close();

            string action; 
            bool showAgain = true;

            do
            {
                Console.WriteLine("");
                Console.WriteLine("1. See all students.");
                Console.WriteLine("2. See student by ID.");
                Console.WriteLine("3. Add new student.");
                Console.WriteLine("0. Exit.");
                Console.WriteLine("");

                action = Console.ReadLine();

                switch (action)
                {
                    case "1":
                        foreach (Student item in students)
                            item.Show();
                        break;

                    case "2":
                        Console.Write("ID = ");
                        int id = int.Parse( Console.ReadLine() ); // TODO: is valid int?

                        foreach (Student item in students)
                            if (item.Id == id)
                                item.Show();
                        break;

                    case "3":
                        Console.WriteLine("ID-Name");

                        Student newStudent = new Student(Console.ReadLine());
                        students.Add(newStudent);

                        StreamWriter writer = new StreamWriter(_filename, true);
                        writer.WriteLine(newStudent);
                        writer.Close();

                        break;

                    case "0":
                        Console.WriteLine("Bye!");
                        showAgain = false;

                        break;

                    default:
                        Console.WriteLine("Wrong action!");
                        break;

                }
            }
            while (showAgain);
        }
    }

    class Student
    {
        public int Id;
        public string Name;

        public Student(string line)
        {
            string[] fields = line.Split('-');

            Id = int.Parse(fields[0]);
            Name = fields[1];
        }

        public void Show()
        {
            Console.WriteLine(Id + ". " + Name);
        }
    }
}

我假设您的数据采用“ID-Name”格式,例如:

1-Alexander
2-Brian
3-Christian

我逐行加载文件并传递给 Student 类,该类将构造函数文本数据转换为更友好的形式。接下来,应用程序显示界面,直到用户写入“0”。

于 2012-12-10T20:59:00.560 回答
0

假设您没有处理大量的学生文件,并且基于您想要进行多个查询,我不会每次都逐行读取文本文件。

而是创建一个学生类,在初始化时读取文件一次,然后从数据中创建一个列表<学生>。然后就可以用 LinQ 查询了

于 2012-12-10T20:48:28.010 回答