iT邦幫忙

0

C#初學者問題

原本可以按照遞增排序,但只要多宣告下面那個新的陣列就整個跑掉,請問原因?

        int[] arr = new int[20];
        Random r = new Random();
        int count;
        for (int count1 = 0; count1 <= 19; count1++)
        {
            for (count = 0; count <= 19; count++)
            {
                arr[count] = r.Next(1, 21);
            }
            Array.Sort(arr);
            Console.Write(arr[count1]);
            Console.Write(" ");
            if (count1 % 5 == 4)
                Console.WriteLine();
        }
       
        int[] NewArr = new int[20];
        
        Console.Read();   
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

4
小魚
iT邦大師 1 級 ‧ 2019-10-23 23:46:24
最佳解答

我看不懂,
你新的陣列沒有做任何事啊,
跑掉什麼?

我倒是覺得你前面寫得有點奇怪...

我猜你要的是這樣吧...

int[] arr = new int[20];
Random r = new Random();
int count;
for (count = 0; count <= 19; count++)
    arr[count] = r.Next(1, 21);
Array.Sort(arr);
for (count = 0; count <= 19; count++)
{
    Console.Write(arr[count]);
    Console.Write(" ");
    if (count % 5 == 4)
        Console.WriteLine();
}

https://ithelp.ithome.com.tw/upload/images/20191024/201056942p8uv0kujw.png

賀 ! 小魚大大, 通靈技能滿點 !
/images/emoticon/emoticon42.gif/images/emoticon/emoticon42.gif/images/emoticon/emoticon42.gif
技術問答區果然是通靈技能的修鍊場 !
/images/emoticon/emoticon01.gif

小魚 iT邦大師 1 級 ‧ 2019-10-24 18:34:13 檢舉

darwin0616
我明年打算參加通靈王大賽,
聽說有個姓葉的蠻強的,
你有興趣來參加嗎?

我要發表回答

立即登入回答