我在连接 2 个代码块时遇到问题。 我有这段代码可以生成 4 种随机颜色。
static Color[] colors = { Color.Red, Color.Green, Color.Yellow, Color.Purple}; // Escolher as cores disponiveis
static Color GetRandomColor()
{
var random = new Random();
return colors[random.Next(colors.Length)];
}
我认为代码很好,但如果不好,请告诉我。
我需要将其与此代码关联:
List<Utente> ListaUtente = new List<Utente>();
ListaUtente.Add(new Utente(22, "Pedro", 555444333, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b7b6e6f79644b6c666a626725686466" rel="noreferrer noopener nofollow">[email protected]</a>"));
ListaUtente.Add(new Utente(22, "Lucas", 555444333, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e524b5d5f4d070f7e59535f5752105d5153" rel="noreferrer noopener nofollow">[email protected]</a>"));
ListaUtente.Add(new Utente(22, "Rodrigo", 555444333, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="07756863756e6068373747606a666e6b2964686a" rel="noreferrer noopener nofollow">[email protected]</a>"));
ListaUtente.Add(new Utente(22, "Gaspar", 555444333, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b3c3a282b3a291b3c363a323775383436" rel="noreferrer noopener nofollow">[email protected]</a>"));
ListaUtente.Add(new Utente(22, "Assis", 555444333, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e5f4d4d574d7e59535f5752105d5153" rel="noreferrer noopener nofollow">[email protected]</a>"));
我需要程序输出其中一种随机颜色(紫色、红色、黄色和绿色)并将其写在屏幕上。
类似于:
Lucas - Green
Rodrigo - Red
如何链接这两个 block ?我可以完全访问列表以及“Utente”类属性。
最佳答案
我不确定您在这里寻找什么。您可以将 Color 属性添加到 Utente 类并更新构造函数,以便可以调用它
ListaUtente.Add(new Utente(22, "Pedro", 555444333, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="69190c0d1b06290e04080005470a0604" rel="noreferrer noopener nofollow">[email protected]</a>", GetRandomColor()));
然后
foreach(Utente utente in ListaUtente)
{
Console.WriteLine(utente.Name + " " + utente.Color);
}
关于c# - 将随机生成的颜色分配给值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43878184/