C#怎样为一个交错数组赋值?注意:不是初始化,是通过Console.Read等形式赋值!
附代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;nam...
附代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
class Student
{
public int num;
public string name;
public int age;
public double score;
}
static void Main(string[] args)
{
Console.WriteLine("请输入班级的数目:");
int c;
c = Convert.ToInt32(Console.ReadLine());
Student[][] stu = new Student[c][];
for (int i = 0; i < c; i++)
{
Console.Write("请输入班级序号:");
int e = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入该班的人数:");
int d = Convert.ToInt32(Console.ReadLine());
stu[i] = new Student[d];
Console.WriteLine("请输入每个班的每个学生的信息:");
for (int f = 0; f < d; f++)
{
Console.Write("学号:");
stu[i][f].num = Convert.ToInt32(Console.ReadLine());
Console.Write("姓名:");
stu[i][f].name = Console.ReadLine();
Console.Write("年龄:");
stu[i][f].age = Convert.ToInt32(Console.ReadLine());
Console.Write("成绩:");
stu[i][f].score = Convert.ToDouble(Console.ReadLine());
}
}
Console.WriteLine("信息录入完毕");
}
}
}
编译能通过,但运行时总提示:"未将对象引用设置到对象的实例。"我实在搞不懂了!已经被搞得焦头烂额了!这个地方老是过不去!问题补充:
每当运行到:stu[i][f].num = Convert.ToInt32(Console.ReadLine());
或是stu[i][f].name = Console.ReadLine();这些地方时就出错!
谁能帮帮我呀?我快郁闷死了! 展开
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
class Student
{
public int num;
public string name;
public int age;
public double score;
}
static void Main(string[] args)
{
Console.WriteLine("请输入班级的数目:");
int c;
c = Convert.ToInt32(Console.ReadLine());
Student[][] stu = new Student[c][];
for (int i = 0; i < c; i++)
{
Console.Write("请输入班级序号:");
int e = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入该班的人数:");
int d = Convert.ToInt32(Console.ReadLine());
stu[i] = new Student[d];
Console.WriteLine("请输入每个班的每个学生的信息:");
for (int f = 0; f < d; f++)
{
Console.Write("学号:");
stu[i][f].num = Convert.ToInt32(Console.ReadLine());
Console.Write("姓名:");
stu[i][f].name = Console.ReadLine();
Console.Write("年龄:");
stu[i][f].age = Convert.ToInt32(Console.ReadLine());
Console.Write("成绩:");
stu[i][f].score = Convert.ToDouble(Console.ReadLine());
}
}
Console.WriteLine("信息录入完毕");
}
}
}
编译能通过,但运行时总提示:"未将对象引用设置到对象的实例。"我实在搞不懂了!已经被搞得焦头烂额了!这个地方老是过不去!问题补充:
每当运行到:stu[i][f].num = Convert.ToInt32(Console.ReadLine());
或是stu[i][f].name = Console.ReadLine();这些地方时就出错!
谁能帮帮我呀?我快郁闷死了! 展开
1个回答
展开全部
for (int f = 0; f < d; f++)
{
stu[i][f] = new Student();
Console.Write("学号:");
stu[i][f].num = Convert.ToInt32(Console.ReadLine());
Console.Write("者肢姓名败旦:");
stu[i][f].name = Console.ReadLine();
Console.Write("年龄:");
stu[i][f].age = Convert.ToInt32(Console.ReadLine());
Console.Write("成绩:察嫌扰");
stu[i][f].score = Convert.ToDouble(Console.ReadLine());
}
注意:在输入学号前写上
stu[i][f] = new Student();
{
stu[i][f] = new Student();
Console.Write("学号:");
stu[i][f].num = Convert.ToInt32(Console.ReadLine());
Console.Write("者肢姓名败旦:");
stu[i][f].name = Console.ReadLine();
Console.Write("年龄:");
stu[i][f].age = Convert.ToInt32(Console.ReadLine());
Console.Write("成绩:察嫌扰");
stu[i][f].score = Convert.ToDouble(Console.ReadLine());
}
注意:在输入学号前写上
stu[i][f] = new Student();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询