【问题标题】:OOP Array filtering using a string?使用字符串进行 OOP 数组过滤?
【发布时间】:2023-04-02 21:59:01
【问题描述】:

所以我对 C# OOP 还是很陌生,而且我刚刚在大学里掌握了这种编程风格,并且遇到了数组。现在,我了解了如何创建它们以及它们的一般用途,但我不明白如何使用字符串过滤它们。

对于一个小的背景故事,这是我被要求做的一部分;

在我的部分项目中,我需要创建三个数组,设置主题信息,然后通过显示方法将其输出给用户。我还需要使用 _findExamIndex(根据给定的考试 ID 在考试数组中搜索结果)列出考试,然后使用 _findStudentIndex(在学生列表中搜索学生)来选择个别学生结果。

我遇到的问题是使用字符串 _topicName 过滤 _exams 数组的结果,我不确定如何去做,因为我不熟悉使用数组作为存储数据的方式,并且会非常感谢您的帮助,非常感谢。

(方法代码)

public void viewTopicsResults(string _topicName,List<Results> _results, List<Student> _students, Exam[] _exams, DisplayClass.Display _theDisplay)
{
        int[] columnWidths = { 3, 20 };
        char[] charactersInTheSpaces = { ' ', ' ' };
        string[] topicInfo = new string[6];

        topicInfo[0] = "Student";
        topicInfo[1] = "Subject";
        topicInfo[2] = "Topic";
        topicInfo[3] = "Style";
        topicInfo[4] = "Date";
        topicInfo[5] = "Score";


        //Ordering the table by topic.      
        _theDisplay.writeLineWithTab(topicInfo, columnWidths, charactersInTheSpaces);


        foreach(Results result in _results)
        {
            int examID = _theDisplay.userInputAsInteger("Select a result");
            _findExamIndex(examID, _exams);
             int studentID = _theDisplay.userInputAsInteger("Please select a users result");
            _findStudentIndex(studentID, _students);

             //filter on the _exams' Topic == '_topicName'

        }
}

(这个方法会被main方法调用)

【问题讨论】:

  • 主题是考试的属性,就像考试ID一样?然后看看_findExamIndex的实现。它的解决方式应该有一个很好的资源来说明如何使用 Topic 而不是 ExamID。

标签: c# arrays oop filtering


【解决方案1】:

就这样去做吧

    foreach(Results result in _results)
    {
        int examID = _theDisplay.userInputAsInteger("Select a result");
        _findExamIndex(examID, _exams);
         int studentID = _theDisplay.userInputAsInteger("Please select a users result");
        _findStudentIndex(studentID, _students);

         //filter on the _exams' Topic == '_topicName'
         exam[]  exams  = FindExamByTopic("youtopic",_exams)

    }


  private Exam[] FindExamByTopic(string topic, Exam[] _exams)
    {
         return _exams.Where(ex => ex.Topic == topic).ToArray();  
    }


  //here without using  linq 

     private Exam[] FindExamByTopicSimple(string topic, Exam[] _exams)
    {
        List<Exam>  exams = new List<Exam>();

        for (int i = 0; i < _exams.Length; i++)
        {
            if (_exams[i].Topic == topic)
            {
                exams.Add(_exams[i]); 
            }
        }
        return exams.ToArray();   
    }

希望对你有所帮助

【讨论】:

    【解决方案2】:

    非常感谢,经过深思熟虑、更改和调整代码,我想出了这个解决方案;

    char[] padding = { ' ', ' ', ' ', ' ', ' ', ' ' };
            int[] widths = { 20, 9, 26, 14, 11, 4 };
            string[] topicInfo = { "Student", "Subject", "Topic", "Style", "Date", "Score" };
    
            _theDisplay.clearScreen();
            _theDisplay.writeLineWithTab(topicInfo, widths, padding);
    
            for (int i = 0; i < _results.Count; i++)
            {
                int examIndex = _findExamIndex(_results[i].ExamRegisterNos, _exams);
                int studentIndex = _findStudentIndex(_results[i].StudentIDNumber, _students);
    
                if (_exams[examIndex].Topic == _topicName)
                {
                    topicInfo[0] = Convert.ToString(_students[studentIndex].studentName());
                    topicInfo[1] = _exams[examIndex].Subject;
                    topicInfo[2] = _exams[examIndex].Topic;
                    topicInfo[3] = _exams[examIndex].StyleOfExam;
                    topicInfo[4] = Convert.ToString(_results[i].TakenOn.ToShortDateString());
                    topicInfo[5] = Convert.ToString(_results[i].IntGradeOutOf100 + "%");
    
                    _theDisplay.writeLineWithTab(topicInfo, widths, padding);
                }
            }
    

    感谢您的帮助,非常感谢。

    【讨论】:

      猜你喜欢
      • 2021-06-06
      • 2019-04-04
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 2017-07-15
      • 2022-07-04
      • 1970-01-01
      相关资源
      最近更新 更多