实例在方法内写入数据,怎么传递到函数外

d1135130491 2017-11-16 10:29:13
声明我是学生,老师给的题目: 建立主菜单,实现功能导航,利用对象数组实现学生信息的添加、修改、删除和查询。
目前没有学到类的继承。运行程序 修改功能的时候,

知识有限我该怎么解决这个问题呢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 实验4
{
public class Student//学生类
{
string name;
string id;
string english;
string chinese;
string math;

public string Name
{
set { name = value; }
get { return name; }
}
public string Id
{
set { id = value; }
get { return id; }
}
public string English
{
set { english = value; }
get { return english; }
}
public string Chinese
{
set { chinese = value; }
get { return chinese; }
}
public string Math
{
set { math = value; }
get { return math; }
}

}

class manager//管理类
{
static int num = 0;
Student[] student = new Student[10];
public void addStudent()//添加学生信息
{
student[num] = new Student();
Console.Write("\t\t\t\t请输入姓名:");
student[num].Name = Console.ReadLine();
Console.Write("\t\t\t\t请输入学号:");
student[num].Id = Console.ReadLine();
Console.Write("\t\t\t\t请输入英语成绩:");
student[num].English = Console.ReadLine();
Console.Write("\t\t\t\t请输入语文成绩:");
student[num].Chinese = Console.ReadLine();
Console.Write("\t\t\t\t请输入数学成绩:");
student[num].Math = Console.ReadLine();
Console.WriteLine("\t\t\t\t**添加成功!**");
num++;
}

public void alterStudent()
{
string xh;
string item;
Console.WriteLine("修改成绩,需要输入学号:");
xh = Console.ReadLine();
for (int i = 0; i <= num; i++)
{
if (student[num].Id == xh)
{
Console.WriteLine("请输入要修改的项目:");
item = Console.ReadLine();
if (item == "英语")
{
Console.WriteLine("请输入新成绩:");
student[num].English = Console.ReadLine();
}
else if (item == "语文")
{
Console.WriteLine("请输入新成绩:");
student[num].Chinese = Console.ReadLine();
}
else if (item == "数学")
{
Console.WriteLine("请输入新成绩:");
student[num].Math = Console.ReadLine();
}
}
}
}

internal void addStudent(ref Student[] student)
{
throw new NotImplementedException();
}
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 实验4
{
class Program
{
static void Main(string[] args)
{
int functionSelect;
while (true)
{
Console.WriteLine("\t\t\t*********************************");
Console.WriteLine("\t\t\t*\t学生信息管理系统\t*");
Console.WriteLine("\t\t\t*\t\t\t\t*"); ;
Console.WriteLine("\t\t\t*\t\t\t\t*");
Console.WriteLine("\t\t\t* 1.添加 2.修改 3.删除 4.查询 *");
Console.WriteLine("\t\t\t*********************************");
functionSelect = Convert.ToInt32(Console.ReadLine());
manager m = new manager();
switch (functionSelect)
{
case 1:
m.addStudent();
break;
case 2:
m.alterStudent();
break;
case 3:
break;
case 4:
break;
}
}
}
}
}
...全文
174 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
smileruner 2017-11-16
  • 打赏
  • 举报
回复
还可以考虑用dictionary,比数组灵活。
smileruner 2017-11-16
  • 打赏
  • 举报
回复
我建议你用List来实现学生数据,List长的不是固定的。
smileruner 2017-11-16
  • 打赏
  • 举报
回复
虽然你声明了数组,但是数组成员并未填充,是空的。
帅猪儿 2017-11-16
  • 打赏
  • 举报
回复
其实很简单,你在使用alterStudent()函数的时候,在循环的之前,并没有实例化student类,且没有给student类赋值,如下 string xh; string item; Console.WriteLine("修改成绩,需要输入学号:"); xh = Console.ReadLine(); 因此,你在调用 student[num].Id 的时候,如 if (student[num].Id == xh), student[num].Id为空,自然就会弹出错误。 建议你单步调试该函数,并在调试过程中监控student[0]的值及student[0].id的只,就可以发现调用之是否为空了。 或者你在循环的过程中,将调用函数改为 for (int i = 0; i <= num; i++) { //增加判断 if(student[num].Id==null) continue;//或者break; if (student[num].Id == xh) { Console.WriteLine("请输入要修改的项目:"); item = Console.ReadLine(); if (item == "英语") { Console.WriteLine("请输入新成绩:"); student[num].English = Console.ReadLine(); } else if (item == "语文") { Console.WriteLine("请输入新成绩:"); student[num].Chinese = Console.ReadLine(); } else if (item == "数学") { Console.WriteLine("请输入新成绩:"); student[num].Math = Console.ReadLine(); } } }

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧