1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//宣告撲克牌一維陣列
int[] card = new int[52];
//初始化陣列值
for(int i = 0; i < card.Length; i++) {
card[i] = i+1;
}
int temp, rndTemp;
Random rnd = new Random(); //宣告一個產生隨機數
for(int i = 0; i < card.Length; i++) {
//產生0~51之間的亂數
rndTemp = rnd.Next(0, card.Length);
//將i位置上的牌,和產生的隨機數rndTemp位置的牌 交換
temp = card[i];
card[i] = card[rndTemp];
card[rndTemp] = card[i];
}
for(int i = 0; i < card.Length; i++) {
Console.WriteLine($"第{i+1}張牌是{card[i]}");
}
[C# 練習] 洗牌程式
This post is licensed under CC BY 4.0 by the author.
