Hỏi đáp
Chia sẻ kiến thức, cùng nhau phát triển
Các bạn kiểm tra giúp mình đoạn code này nhé. Thanks!
00:39 23-04-2018
405 lượt xem
5 bình luận
00:40 26-04-2018
Mình f10 thì các kết quả trong list đều random tuy nhiên kết quả in ra màn hình lại chỉ cho lần lặp đầu tiên. Các bạn kiểm tra giúp mình nhé. Thanks
static List<int> Random_KQ_Hang_Ngay()
{
List<int> lstKq = new List<int>();
Random rd = new Random();
for (int i = 0; i < 27; i++)
{
lstKq.Add(rd.Next(0, 99));
}
return lstKq;
}
static void In_KQ_Hang_Ngay(List<int> lstKq)
{
string kQ = "";
foreach (var item in lstKq)
{
kQ += item + " ";
}
Console.WriteLine(kQ);
}
static void Main(string[] args)
{
Console.Write("So ngay ban muon random ket qua: ");
int soNgay = int.Parse(Console.ReadLine());
List<int> lstKq;
for (int i = 1; i <= soNgay; i++)
{
lstKq = new List<int>();
lstKq = Random_KQ_Hang_Ngay();
In_KQ_Hang_Ngay(lstKq);
}
Console.ReadLine();
}
Mình không nhầm là ý bạn mỗi kết quả in ra cũng là random, bạn chạy thử code mình xem.
kQ += item.ToString() + " ";
lstKq = Random_KQ_Hang_Ngay(); -> lstKq = new List<int>() trước rồi gán kết quả thử bạn