2016-10-03 109 views
1

我有一個數組,其中用戶將插入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]); 
     } 
+0

應該如何的.Net知道你想要什麼'Job'類型看起來像在控制檯上?你需要一個告訴它的ToString()方法。 –

+0

當每個作業具有不同的屬性或字段(或兩者)時,您正在像一個單獨的變量對待類。每一個都需要處理。你可以Console.WriteLine(jobArray [0] .Job.Description)雖然 –

回答

2

我只是在記事本中記這件事,但在你的工作班級,你應該有類似以下的程序知道要打印什麼:

public override string ToString() 
{ 
    return this.Description + ", Hours: " + this.Hours.ToString() + ", Rate: " + this.Rate.ToString(); 
} 

然後你會做

Console.WriteLine(job1.ToString()); 
+0

或jobArray [0] .ToString();-) –

+0

@Rob是正確的,我編輯我的答案 – KingDan