3

私は次のコードを持っています:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Maman15cs
{
    public class ClassRoom
    {
        public string ClassNumber;
        public int NumberofPlaces;
        public int[,] DayandHour = new int[6,8];

        public void AddClassRoom()
        {
            Console.WriteLine("Enter the Class number, the Number of places\n");
            ClassNumber = Console.ReadLine().ToString();
            NumberofPlaces = int.Parse(Console.ReadLine());
            Console.WriteLine("Good, now enter the Day(1, 2, 3, 4, 5, 6) and after that you put the courses' number that are that day (In Order)");
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 8; j++)
                {

                    DayandHour[i,j] = int.Parse(Console.ReadLine());

                }

            }
        }

    }

    public class Course
    {
        public string CourseName;
        public int CourseNumber;
        public int StudentsNumber;
        public string TeacherName;
        public string ClassNumber;

        // Tuple<string, int, int, string, string>

        public void AddCourse(Course *course)
        {
            Console.WriteLine("Enter the Course's name, course's number, students number, teacher's name, and class' number\n");
            CourseName = Console.ReadLine().ToString();
            CourseNumber = int.Parse(Console.ReadLine());
            StudentsNumber = int.Parse(Console.ReadLine());
            TeacherName = Console.ReadLine().ToString();
            ClassNumber = Console.ReadLine().ToString();

        }
    }

    public class Program
    {

         void Main()
        {
            Course[] course = new Course[1000];
            ClassRoom[] classroom = new ClassRoom[1000];
            Course* coursePointer;


            int actionChoice;
            int courseCount = 0, classroomCount = 0;

             loop:

             Console.WriteLine("What do you want to do? (Enter number): \n  1) Add a new Course \n 2)Add a new class room \n 3)Add an existing class to an existing classroom \n 4)Read the information of a specific classroom \n 5)Read the information of all the classrooms \n 6)Read the information of a specific course \n 7)Delete a specific course \n 8)Update courses in the Time Table \n 9)Exit the program  \n");
             actionChoice = int.Parse(Console.ReadLine());

             switch (actionChoice)
             {

                 case 1: //Add a new Course

                    // course[classroomCount].AddCourse();

                   break;


             }

             goto loop;

        }
    }
}

そして、 AddCourse 関数がポインターを返すか使用して変数コースに入力を追加するようにしたいのですが、 list<> のようなことをいくつか試しましたが、これについては経験がありません。

4

3 に答える 3

3

AddCourse新規作成してCourse返却するように変更してください。

public Course AddCourse()
{
     var course = new Course();
     course.CourseName = Console.ReadLine().ToString();
     // ... more readlines
     return course;
 }

主に:

List<Course> courses = new List<Course>();

case 1: courses.Add(AddCourse()); break;
于 2013-07-29T00:34:27.407 に答える
1

最初に、すべてのコースを保持するリストを設定します。必ずしも配列である必要はありません (本当に配列が必要でない限り)。

List<Course> Courses = new List<Courses>();

AddCourse メソッドを変更して、新しくインスタンス化された Course オブジェクトを返します。

Public Course AddCourse(){

Course newCourse = new Course();
<logic to populate the object>

return newCourse;

}

コースを追加するループ内で、次のようにします。

Courses.add(AddCourse());

次に、ループ構造を使用してすべてのコースを通過するか、linq を使用して必要な特定のコースを取得できます。

- -編集 -

Course クラスの設定方法に固執しているため (ベスト プラクティスではありません)、AddCourse メソッドを次のように変更する必要があります。

 public class Course
    {
        public string CourseName;
        public int CourseNumber;
        public int StudentsNumber;
        public string TeacherName;
        public string ClassNumber;
        public void AddCourse()
        {
            Console.WriteLine("Enter the Course's name, course's number, students number, teacher's name, and class' number\n");
            this.CourseName = Console.ReadLine().ToString();
            this.CourseNumber = int.Parse(Console.ReadLine());
            this.StudentsNumber = int.Parse(Console.ReadLine());
            this.TeacherName = Console.ReadLine().ToString();
            this.ClassNumber = Console.ReadLine().ToString();

        }
    }

次に、ループ メソッドの呼び出しは次のようにする必要があります。

Course NewCourse = new Course();
Courses.Add(NewCourse.AddCourse());
于 2013-07-29T00:34:30.423 に答える