C# 如何使用变量';有不同的方法吗?
我正在用C#编写一个程序,这是一个控制台应用程序,我遇到了一个问题 到目前为止,我有五个班:主班是C# 如何使用变量';有不同的方法吗?,c#,console-application,C#,Console Application,我正在用C#编写一个程序,这是一个控制台应用程序,我遇到了一个问题 到目前为止,我有五个班:主班是程序,另外四个班是教师,学生,课程,班 我的问题是,我从用户那里收到信息,而这些信息都在另一种方法中 我希望能够使用从用户那里获得的输入,并以不同的方法使用它来编辑输入 简单地说程序应该能够编辑输入,但我不能让它工作?!? 这是我写的代码: struct Students { public string name; public string family; public int
程序
,另外四个班是教师
,学生
,课程
,班
我的问题是,我从用户那里收到信息,而这些信息都在另一种方法中
我希望能够使用从用户那里获得的输入,并以不同的方法使用它来编辑输入
简单地说程序应该能够编辑输入,但我不能让它工作?!?
这是我写的代码:
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();
}
}
struct学生
{
公共字符串名称;
公共字符串系列;
公共int ID;
公共弦专业;
公共int学生代码;
}
班级学生
{
公共静态无效显示学生()
{
Console.Clear();
Console.WriteLine(“这是学生部分:\n”);
Console.WriteLine(“====================================================”);
Console.WriteLine(“输入要使用的节的编号:\n”);
控制台。写入线(“1提交”);
Console.WriteLine(“2编辑”);
控制台。写线(“3回”);
字符串选项=Console.ReadLine();
开关(选择)
{
案例“1”:
接受者();
打破
案例“2”:
打破
案例“3”:
Program.mainShow();
打破
违约:
Console.Clear();
Console.WriteLine(“请输入范围编号。\n按任意键继续…”);
Console.ReadKey();
ShowStudent();
打破
}
}
公共静态无效接收学生()
{
Console.Clear();
int n;
Console.WriteLine(“您想输入多少名学生?”);
n=Convert.ToInt32(Console.ReadLine());
学生[]st=新生[n];
对于(int i=0;i
在main中调用showtudent()
方法,即program.cs
(void main
)
我想在不同的方法中使用数组值
感谢您的评论
谢谢大家。返回方法接收到的学生从用户处收集的数据,并将这些数据用作另一个方法的输入 将ReceiveStudent更改为:
公共静态学生[]ReceiveStudent()
并将返回的数组用作其他方法的输入:
Students[] students = ReceiveStudent();
otherMethod(students) // Do other work on data
顺便说一句,你的命名可能需要一些工作。例如,你的struct Students实际上并没有为一组学生建模,而是只为一组学生建模
编辑:
struct学生
{
公共字符串名称;
公共字符串系列;
公共int ID;
公共弦专业;
公共int学生代码;
}
班级学生
{
公共静态无效显示学生()
{
Console.Clear();
Console.WriteLine(“这是学生部分:\n”);
Console.WriteLine(“====================================================”);
Console.WriteLine(“输入要使用的节的编号:\n”);
控制台。写入线(“1提交”);
Console.WriteLine(“2编辑”);
控制台。写线(“3回”);
字符串选项=Console.ReadLine();
Students[]Students=null;
开关(选择)
{
案例“1”:
学生=接受学生();
打破
案例“2”:
打破
案例“3”:
Program.mainShow();
打破
违约:
Console.Clear();
Console.WriteLine(“请输入范围编号。\n按任意键继续…”);
Console.ReadKey();
ShowStudent();
打破
}
//在这里使用变量“students”
//请记住检查null,因为它可能在开关情况下没有发生突变。
如果(学生!=null)
{
//对这里的学生做点什么…现在就打印出来。
Console.WriteLine(students.ToString());
}
}
公共静态学生[]接收学生()
{
Console.Clear();
int n;
Console.WriteLine(“您想输入多少名学生?”);
n=Convert.ToInt32(Console.ReadLine());
学生[]st=新生[n];
对于(int i=0;i
您是否考虑过从Receive
方法返回st
数组,并将其存储在main方法中以备将来使用?@EugenePodskal您确定它有效吗???因为我认为当接收完成时,该值将更改为null。感谢您提供的解决方案。。。我刚开始学英语
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;
}
}