如何从函数中检索输入
本文关键字:检索 输入 函数 | 更新日期: 2023-09-27 18:25:12
我有下一个代码:
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<>但我对此没什么经验。
更改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;
从C:)切换到C#后,我也遇到了类似的问题
首先,可以将Course[] course = new Course[1000];
替换为var course = new List<Course>();
。List<T>
对于大多数场景来说要好得多——它没有确切的大小,你可以在任何位置"动态"添加任何数量的元素。
其次,所有类实例都作为引用传递。指针只能在一些罕见的场景中使用。
第三。goto
几乎从未在C#中使用过。语言中有大量的循环、枚举器等-foreach,而对于
最后。在你的情况下,我会这样做:
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的新实例。switch
中的default
选择器。return
退出应用程序。CCD_ 11作为课程的存储器。new Course()
命令使用自动创建的隐式构造函数,因为未定义任何构造函数。
首先,设置一个列表来保存所有课程,而不一定是一个数组(除非你真的需要一个数组):
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来获得您需要的特定课程。
---编辑--
由于你一直坚持课程类的设置方式(顺便说一句,这不是最佳实践),你需要将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());