我用C#编写程序,控制台应用程序和我遇到了问题。
到目前为止,我已经有五个班级:主要班级为Program
,另外四个为Teacher
,Student
,Course
,{ {1}}。
我的问题是我收到了用户提供的信息,而且信息都是另一种方法。
我希望能够使用我从用户那里获得的输入,并以不同的方法使用它来编辑输入。
简单地说该程序应该能够编辑输入并且我无法使其工作?!? 这是我写的代码:
Class
在main中调用struct Students
{
public string name;
public string family;
public int ID;
public string Major;
public int studentCode;
}
class Student
{
public static void ShowStudent()
{
Console.Clear();
Console.WriteLine("This is Student's Section:\n");
Console.WriteLine("====================================");
Console.WriteLine("Enter the Number Of the Section you want to Work with:\n");
Console.WriteLine("1 Submit");
Console.WriteLine("2 Edit");
Console.WriteLine("3 Back");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
ReciveStudent();
break;
case "2":
break;
case "3":
Program.mainShow();
break;
default:
Console.Clear();
Console.WriteLine("Please Enter INrange Number.\nPress any Key to Continue....");
Console.ReadKey();
ShowStudent();
break;
}
}
public static void ReceiveStudent()
{
Console.Clear();
int n;
Console.WriteLine("How Many Student's you Want to Enter?");
n = Convert.ToInt32(Console.ReadLine());
Students[] st = new Students[n];
for (int i = 0; i < n; i++)
{
Console.WriteLine("Enter Student ID:");
st[i].ID = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Student CODE:");
st[i].studentCode = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Student Name:");
st[i].name = Console.ReadLine();
Console.WriteLine("Enter Student Family:");
st[i].family = Console.ReadLine();
Console.WriteLine("Enter Student Major:");
st[i].Major = Console.ReadLine();
}
ShowStudent();
}
}
方法,即ShowStudent()
(program.cs
)。
我想以不同的方法使用数组值。
感谢任何评论...
谢谢你们。
答案 0 :(得分:1)
返回RecieveStudent方法从用户收集的数据,并将数据用作另一种方法的输入。
将RecieveStudent更改为:public static Students[] RecieveStudent()
public static Students[] RecieveStudent() {
// your code here as is
return st;
}
并使用返回的数组作为其他方法的输入:
Students[] students = ReceiveStudent();
otherMethod(students) // Do other work on data
不过,你的命名可以用一些工作。例如。你的结构学生实际上并没有为一组学生建模,但只有一个。
编辑:
struct Students
{
public string name;
public string family;
public int ID;
public string Major;
public int studentCode;
}
class Student
{
public static void ShowStudent()
{
Console.Clear();
Console.WriteLine("This is Student's Section:\n");
Console.WriteLine("====================================");
Console.WriteLine("Enter the Number Of the Section you want to Work with:\n");
Console.WriteLine("1 Submit");
Console.WriteLine("2 Edit");
Console.WriteLine("3 Back");
string choice = Console.ReadLine();
Students[] students = null;
switch (choice)
{
case "1":
students = ReciveStudent();
break;
case "2":
break;
case "3":
Program.mainShow();
break;
default:
Console.Clear();
Console.WriteLine("Please Enter INrange Number.\nPress any Key to Continue....");
Console.ReadKey();
ShowStudent();
break;
}
// Use variable 'students' here
// Remember to check for null as it might not have been mutated in the switch-case.
if (students != null)
{
// Do something with students here... just printing it for now.
Console.WriteLine(students.ToString());
}
}
public static Students[] ReceiveStudent()
{
Console.Clear();
int n;
Console.WriteLine("How Many Student's you Want to Enter?");
n = Convert.ToInt32(Console.ReadLine());
Students[] st = new Students[n];
for (int i = 0; i < n; i++)
{
Console.WriteLine("Enter Student ID:");
st[i].ID = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Student CODE:");
st[i].studentCode = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Student Name:");
st[i].name = Console.ReadLine();
Console.WriteLine("Enter Student Family:");
st[i].family = Console.ReadLine();
Console.WriteLine("Enter Student Major:");
st[i].Major = Console.ReadLine();
}
ShowStudent();
return st;
}
}