我有一個數組,其中用戶將插入5個作業,其中包括描述,完成時間和小時工資。對於新手問題抱歉,因爲我是這種語言的新手。任何幫助,將不勝感激。c#數組不打印內容
private static void EnterJobs()
{
//string inputString;
for (int i = 0; i < jobArray.Length; i++)
{
Job job = new Job();
Console.WriteLine("Job " + i);
Console.WriteLine("Enter a job description.");
job.Description = Console.ReadLine();
Console.WriteLine("Enter the amount of hours required to complete the job.");
job.hoursToComplete = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the hourly rate for the job.");
job.hourlyRate = Convert.ToInt32(Console.ReadLine());
jobArray[i] = job;
}
當我嘗試打印出數組的內容是打印出
DemoJobs.Job
DemoJobs.Job
DemoJobs.Job
DemoJobs.Job
DemoJobs.Job
使用這種循環
for (int i = 0; i < jobArray.Length; i++)
{
Console.WriteLine(jobArray[i]);
}
應該如何的.Net知道你想要什麼'Job'類型看起來像在控制檯上?你需要一個告訴它的ToString()方法。 –
當每個作業具有不同的屬性或字段(或兩者)時,您正在像一個單獨的變量對待類。每一個都需要處理。你可以Console.WriteLine(jobArray [0] .Job.Description)雖然 –