我有下一个代码:
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&lt;&gt;但我对此并不熟悉。
答案 0 :(得分:3)
更改AddCourse
以创建新的Course
并将其返回。
public Course AddCourse()
{
var course = new Course();
course.CourseName = Console.ReadLine().ToString();
// ... more readlines
return course;
}
在Main:
List<Course> courses = new List<Course>();
case 1: courses.Add(AddCourse()); break;
答案 1 :(得分: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以获得你需要的特定课程。
---编辑 -
由于您坚持使用课程类的设置方式(这不是btw的最佳实践),您需要将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());
答案 2 :(得分:1)
从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
,它读取并返回课程的新实例。 default
中的switch
选择器。 return
退出应用。 List<T>
作为课程的存储空间。 new Course()
命令使用自动创建的隐式构造函数,因为没有定义任何构造函数。