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 函数返回或使用指针将输入添加到变量 course 中,我尝试了一些类似 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 回答
1

从 C 切换到 C# 后我遇到了类似的问题 :)

首先,您可以替换Course[] course = new Course[1000];var course = new List<Course>();. List<T>对于大多数场景来说要好得多 - 它没有确切的大小,您可以在任何位置“动态”添加任意数量的元素。

其次,所有类实例都作为引用传递。指针仅在一些罕见的场景中可用。

第三。goto几乎从未在 C# 中使用过。语言中有大量的循环、枚举器等——foreach、while、for

最后的。在你的情况下,我会这样做:

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

    public static Course ReadCourse()
    {
        var rez = new Course();

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

        return rez;
    }
}

public class Program
{   
    void Main()
    {
        var courses = new List<Course>();

        int actionChoice;
        while(1=1)
        {
            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
                    var new_course = Course.ReadCourse();
                    courses.Add(new_course);    
                    break;

                case 9: // Exit
                    return;
                default:
                    Console.WriteLine("Wrong input");
            }
        }
    }
}

这里有什么有趣的。Course.ReadCourse读取并返回 Course 的新实例的静态方法。default中的选择器switchreturn退出应用程序。List<T>作为课程的存储。new Course()command 使用自动创建的隐式构造函数,因为没有定义任何构造函数。

于 2013-07-29T00:41:04.910 回答