2012-12-10 39 views
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; 
      ... 
     } 
     ... 
    } 
    ... 
} 

我想我的代码只是读学生indivially,并转发给我这个部分在一个时间,而不是所有的人都阅读只有一个学生,而不是现在这样做,只是当我按'1)查看学生'时,它只是把所有的人都叫回给我。“学生”几乎说“请输入您想要的学生的姓名或身份证号码视图”。 我目前已经得到了一个随机数字发生器运行的ID号码。C#创建控制台流阅读器使用.txt文件

谢谢你的时间家伙。

+0

输入文件的格式是什么? –

+4

为什么你在地球上使用goto? – mbeckish

+2

建议:删除“goto”。他们是一个禁忌。改用循环。 –

回答

1

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

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; 
      } 
     } 
    } 
} 

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

+2

加上一个说有转(goto)有道理的情况(虽然极为罕见)。 –

+0

这几乎是我所追求的,谢谢。 我所需要做的就是扩大我的12个课程模块,这些模块在中学1,6,6个单元分为2年级,6个单元,然后将它们除以12,以在2年内获得平均%的分数。 但这也是即时通讯之后。 谢谢。 –

0

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

取而代之,创建一个学生类,在init上读取一次文件,并从数据创建一个列表< student>。然后你可以使用LINQ

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名称”格式:

1-Alexander 
2-Brian 
3-Christian 

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