gpt4 book ai didi

c# - 代码适用于一种情况,但不适用于其他情况

转载 作者:太空宇宙 更新时间:2023-11-03 19:57:14 25 4
gpt4 key购买 nike

我声明了 4 个对象数组;

Student[,] s2 =new BSIT[10,4];
s2[studCount,iden] = new BSIT();

Student[,] s3 = new BSCS[10,4];
s3[studCount,iden] = new BSCS();

Student[,] s4 = new MSIT[10,4];
s4[studCount,iden] = new MSIT();

Student[,] s5 = new MSCS[10,4];
s5[studCount,iden] = new MSCS();

然后我写了一些 case switch 语句来询问名称,例如:

case 1://BSIT
{
iden=0;
Console.Write("Enter Name: ");
(s2[studCount,iden]).setName(Console.ReadLine());
do
{
Console.Write("Enter Age(15-30 only): ");
int a = int.Parse(Console.ReadLine());

if(a>=15 && a<=30)
{
(s2[studCount,iden]).setAge(a);
valid=1;
}
else
{
valid=0;
Console.WriteLine("INVALID INPUT. TRY AGAIN.");
}
}
while(valid==0);
}

但是当我尝试在其他对象上输入名称时,例如:

case 2://BSCS
{
iden = 1;
Console.Write("Enter Name: ");
(s3[studCount,iden]).setName(Console.ReadLine());
do
{
Console.Write("Enter Age(15-30 only): ");
int a = int.Parse(Console.ReadLine());

if(a>=15 && a<=30)
{
(s3[studCount,iden]).setAge(a);
valid=1;
}
else
{
valid=0;
Console.WriteLine("INVALID INPUT. TRY AGAIN.");
}
}
while(valid==0);
}

It gives me the System.NullReferenceException: Object Reference not set to instance of object.

是的,基类(称为“Student”)具有所有“Name”setter 和 getter,而派生类 BSIT 和 BSCS 都只从 Student 类中获取“Name”字段。

为什么当我为 BSIT 输入信息时程序运行良好,但为 BSCS、MSIT 和 MSCS 输入信息却没有? MSIT 和 MSCS 遵循与 BSIT 和 BSCS 完全相同的格式,但它们仍然不起作用。

最佳答案

问题:

你只初始化了 1 个元素

s2[studCount,iden] = new BSIT();
//and so on

我想这是 0,所以只要您访问索引 [studCount,0] 上的元素,它就可以正常工作。但不是其他元素。

解决方案:

iden = 1;
Console.Write("Enter Name: ");
s3[studCount,iden] = new BSCS(); //just initialize it before use
(s3[studCount,iden]).setName(Console.ReadLine());

您的场景:

您必须根据Discipline 保存Student。这就是为什么你生成了一个包含 4 列(BSIT、BSCS、MSIT、MSCS)的数组,但你没有像你应该的那样使用它们。您创建了 4 个包含 4 列的数组,现在使用您的方法,您的数组将在最后的某些位置包含空元素。

您的方法应该是:

你的类结构是:

Student
BSIT
BSCS
MSIT
MSCS

所以只需创建一个Student数组,并将不同的子类实例放入其中即可。

Student[,] students = new Student[10,4]; //index 0 is for BSIT, 1 for BSCS and so on..

我只是在这里为 2 个案例编写代码,以帮助您了解工作原理:

case 1://BSIT
{
iden = 0;
Console.Write("Enter Name: ");
//here you'll initialize the object
students[studCount, iden] = new BSIT(); //Student is base, so it can contain all of it's child objects

(students[studCount, iden]).setName(Console.ReadLine());
do
{
Console.Write("Enter Age(15-30 only): ");
int a = int.Parse(Console.ReadLine());

if(a>=15 && a<=30)
{
(students[studCount, iden]).setAge(a);
valid=1;
}
else
{
valid=0;
Console.WriteLine("INVALID INPUT. TRY AGAIN.");
}
}
while(valid==0);
}

case 2://BSCS
{
iden = 1;
Console.Write("Enter Name: ");
(s3[studCount,iden]).setName(Console.ReadLine());

iden = 1;
Console.Write("Enter Name: ");
//here you'll initialize the object
students[studCount, iden] = new BSCS(); //Student is base, so it can contain all of it's child objects (here we're initializing BSCS)

(students[studCount, iden]).setName(Console.ReadLine());
//your code...

现在什么都不会覆盖,所有学科都将位于其指定的列中。

注意:我已经更改了数组的名称,所以请在您的代码中修改它

关于c# - 代码适用于一种情况,但不适用于其他情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32263656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com