簡體   English   中英

C#For循環和數組(練習)

[英]C# For Loop and Array (practice exercise)

我正在嘗試學習C#,並且一直在做一些練習程序。 在此示例中,我嘗試將PracticeArray中的整數傳輸到PracticeArray2,但未成功,而是獲取此作為輸出:

System.Int32[]
System.Int32[]

我的程序代碼如下:

static void Main(string[] args)
    {
        int[] practiceArray = new int[10] {2,4,6,8,10,12,14,16,18,20 };
        int[] practiceArray2 = new int[practiceArray.Length];

        for (int index = 0; index < practiceArray.Length; index++) 
        {
            practiceArray2[index] = practiceArray[index];
        }

        Console.WriteLine(practiceArray);
        Console.WriteLine(practiceArray2);


    }

Console.WriteLine沒有用於輸出復雜對象的任何復雜邏輯,如果它不是字符串,則僅調用ToString()。 您需要使用string.Join等手動連接數組中的值。

例如: Console.WriteLine(string.Join(", ", practiceArray));

int[] practiceArray = new int[10] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
int[] practiceArray2 = new int[practiceArray.Length];

  for (int index = 0; index < practiceArray.Length; index++)
  {
    practiceArray2[index] = practiceArray[index];
  }

  foreach (int pArray in practiceArray)
    Console.Write(pArray + " ");      

  foreach (int pArray2 in practiceArray2)
    Console.Write(pArray2 + " ");

  Console.Read();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM