C# 如何从函数中检索输入

C# 如何从函数中检索输入,c#,function,pointers,C#,Function,Pointers,我有下一个代码: 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;

我有下一个代码:

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;

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间Maman15cs
{
公共课堂
{
公共字符串类号;
公共场所;
公共整数[,]日和小时=新整数[6,8];
公共教室()
{
Console.WriteLine(“输入类号,学号\n”);
ClassNumber=Console.ReadLine().ToString();
NumberofPlaces=int.Parse(Console.ReadLine());
Console.WriteLine(“很好,现在输入当天(1、2、3、4、5、6),然后按顺序输入当天的课程编号”;
对于(int i=0;i<6;i++)
{
对于(int j=0;j<8;j++)
{
DayandHour[i,j]=int.Parse(Console.ReadLine());
}
}
}
}
公共课
{
公共字符串CourseName;
公共国际课程;
公共int学生人数;
公共字符串教师名称;
公共字符串类号;
//元组
公共课程(课程*课程)
{
Console.WriteLine(“输入课程名称、课程编号、学生编号、教师名称和班级编号\n”);
CourseName=Console.ReadLine().ToString();
CourseNumber=int.Parse(Console.ReadLine());
StudentsNumber=int.Parse(Console.ReadLine());
TeacherName=Console.ReadLine().ToString();
ClassNumber=Console.ReadLine().ToString();
}
}
公共课程
{
void Main()
{
课程[]课程=新课程[1000];
教室[]教室=新教室[1000];
课程*课程指南;
国际行动选择;
int courseCount=0,classroomCount=0;
循环:
Console.WriteLine(“您想做什么?(输入数字):\n 1)添加新课程\n 2)添加新教室\n 3)将现有教室添加到现有教室\n 4)读取特定教室的信息\n 5)读取所有教室的信息\n 6)读取特定课程的信息\n 7)删除特定课程\n 8)更新时间表中的课程\n 9)退出程序\n”);
actionChoice=int.Parse(Console.ReadLine());
开关(操作选择)
{
案例1://添加新课程
//课程[classroomCount].AddCourse();
打破
}
后藤环;
}
}
}

我希望AddCourse函数返回或使用指针将输入添加到变量course,我尝试了一些类似list的方法,但对此我没有太多经验。

更改
AddCourse
以创建新的
课程并返回它

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;
List课程=新建列表();
案例1:courses.Add(AddCourse());打破

首先,设置一个列表来容纳所有课程,而不一定是一个数组(除非您确实需要数组):

然后,您可以使用任何循环结构来完成所有课程或linq,以获得您需要的特定课程

---编辑--

由于您一直坚持课程设置的方式(顺便说一句,这不是最佳实践),您需要将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());
然后循环方法中的调用需要如下所示:

 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());

在从C:)切换到C之后,我也遇到了类似的问题

首先,您可以替换
课程[]课程=新课程[1000]带有
var课程=新列表()<代码>列表
对于大多数场景来说都要好得多-它没有精确的大小,您可以在任何位置“动态”添加任意数量的元素

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

第三,<代码>转到
几乎从未在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");
            }
        }
    }
}
公共课
{
公共字符串CourseName;
公共国际课程;
公共int学生人数;
公共字符串教师名称;
公共字符串类号;
公共静态课程ReadCourse()
{
var rez=新课程();
Console.WriteLine(“输入课程名称、课程编号、学生编号、教师名称和班级编号\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();
返回rez;
}
}
公共课程
{   
void Main()
{
var courses=新列表();
国际行动选择;
而(1=1)
{
Console.WriteLine(“您想做什么?(输入数字):\n 1)添加新课程\n 2)添加新教室\n 3)将现有教室添加到现有教室\n 4)读取特定教室的信息\n 5)读取所有教室的信息\n 6)读取特定课程的信息\n 7)删除特定课程\n 8)更新时间表中的课程\n 9)退出程序\n”);
actionChoice=int.Parse(Console.ReadLine());
开关(操作选择)
{
案例1://添加新课程
var new_course=course.ReadCourse();
课程。添加(新课程);
打破
案例9://退出
返回;
违约:
Console.WriteLine(“输入错误”);
}
}
}
}
这里有什么有趣的。静态方法
Course.ReadCourse
读取
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");
            }
        }
    }
}