我試圖使用結構的數組從用戶那裏得到輸入,然後打印:陣列結構的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CA4
{
class Program
{
static void Main(string[] args)
{
StudentDetails[,] student = new StudentDetails[5, 1];
Console.WriteLine("Please enter the unit code:");
student[0, 0].unitCode = Console.ReadLine();
Console.WriteLine("Please enter the unit number:");
student[1, 0].unitNumber = Console.ReadLine();
Console.WriteLine("Please enter first name:");
student[2, 0].firstName = Console.ReadLine();
Console.WriteLine("Please enter last name:");
student[3, 0].lastName = Console.ReadLine();
Console.WriteLine("Please enter student mark:");
student[4, 0].studentMark = int.Parse(Console.ReadLine());
for (int row = 0; row < 5; row++)
{
Console.WriteLine();
for (int column = 0; column < 1; column++)
Console.WriteLine("{0} ", student[row, column]);
}
Console.ReadLine();
}
public struct StudentDetails
{
public string unitCode; //eg CSC10208
public string unitNumber; //unique identifier
public string firstName; //first name
public string lastName;// last or family name
public int studentMark; //student mark
}
}
}
不幸的是輸入所有數據後,我得到:
CA4.Program+StudentDetails
CA4.Program+StudentDetails
CA4.Program+StudentDetails
CA4.Program+StudentDetails
CA4.Program+StudentDetails
它不會崩潰,而不是我輸入的數據獲得上述5行。
我知道它不起作用的原因是我沒有正確使用結構,因爲沒有它們就沒有問題。
有人可以幫助我,並告訴我如何在上面的例子中使用結構。由於
乾杯,
n1te
首先感謝大家的快速響應 說實話,我不必在這裏使用數組,但下一個任務是相似的,我需要在那裏使用數組,所以我想來練習。 在接下來的任務,我必須接受的數據爲: 週數, 主隊名, 客隊名, 勝(1或0) _italic_Your程序必須使用結構數組/ s的存儲數據在內存中,即你的程序必須將文件中的數據作爲結構數組讀取到內存中,並操作數組中的數據。 總共有5周和80場比賽(有16支隊伍) – n1te
這就是爲什麼我想我會在這裏放棄它,所以在下一個任務中更容易。 @Nick Bradley – n1te
這很好,只要你明白,在你發佈的原始代碼中,你沒有填充一個StudentDetails結構體 - 你填寫了五個不同的結構體,但是你只填寫了unitCode第一個(在[0,0])unitNumber在第二個([1,0])第三名在第三..等你沒有完全填寫你所做的任何StudentDetails結構。 –