如何纠正此代码?请帮忙。该代码包含数组,我想找到最大和最小数量。
发生此错误:
未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例。 在D:\ C#\ assign 3.1 \ assign 3.1 \ Program.cs:line 62中的Assign_3._1.ArrayOperations.Maximum() 在D:\ C#\ assign 3.1 \ assign 3.1 \ Program.cs:line 97中的assign_3._1.Program.Main(String [] args)
class Numbers
{
public int iSize;
public int[] arr;
public Numbers()
{
}
public Numbers(int x)
{
iSize = x;
arr = new int[iSize];
}
public Numbers(Numbers obj)
{
iSize = obj.iSize;
arr = obj.arr;
}
public void Accept()
{
int i = 0;
Console.WriteLine("Enter Elements");
for (i = 0; i < iSize; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
}
public void Display()
{
int i = 0;
for (i = 0; i < iSize; i++)
{
Console.WriteLine("{0}", arr[i]);
}
}
}
class ArrayOperations : Numbers
{
public int Maximum()
{
int i = 0;
int iMax = 0;
iMax = arr[0];
for (i = 0; i < iSize; i++)
{
if (arr[i] > iMax)
{
iMax = arr[i];
}
}
return iMax;
}
public int Minimum()
{
int i = 0;
int iMin = 0;
iMin = arr[0];
for (i = 0; i < iSize; i++)
{
if (arr[i] < iMin)
{
iMin = arr[i];
}
}
return iMin;
}
}
class Program
{
static void Main(string[] args)
{
Numbers obj = new Numbers();
obj.Accept();
Numbers obj1 = new Numbers(5);
obj1.Accept();
obj1.Display();
Numbers obj2 = new Numbers(obj1);
obj2.Accept();
obj2.Display();
ArrayOperations obj3 = new ArrayOperations();
obj3.Maximum();
Console.WriteLine("Maximum Number is{0}", obj3.Maximum());
obj3.Minimum();
Console.WriteLine("MInimum Number is{0}", obj3.Minimum());
}
}