如何使用变量';s采用不同的方法
本文关键字:方法 变量 何使用 | 更新日期: 2023-09-27 18:24:46
我正在C#中编写一个程序,控制台应用程序和我遇到了一个问题。
到目前为止,我已经得到了五个类:主类Program
和其他四个Teacher
、Student
、Course
、Class
。
我的问题是,我从用户那里收到信息,而这些信息都是用另一种方法。
我希望能够使用我从用户那里得到的输入,并用不同的方法来编辑输入。
简单地说程序应该能够编辑输入,而我却无法使其工作?!?这是我写的代码:
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
(void main
)。
我想在不同的方法中使用数组值。
感谢任何评论。。。
谢谢大家。
返回方法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
顺便说一句,你的命名可能需要一些工作。例如,你的结构Students实际上并没有为一组学生建模,而是只为一个学生建模。
编辑:
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;
}
}