我使用以下代码生成随机颜色。
static Color[] colors = { Color.Red, Color.Green, Color.Yellow};
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, "pedro@gmail.com"));
ListaUtente.Add(new Utente(22, "Lucas", 555444333, "lucas91@gmail.com"));
ListaUtente.Add(new Utente(22, "Rodrigo", 555444333, "rodrigo00@gmail.com"));
ListaUtente.Add(new Utente(22, "Gaspar", 555444333, "gaspar@gmail.com"));
ListaUtente.Add(new Utente(22, "Assis", 555444333, "assis@gmail.com"));
并在屏幕上显示如下:用户1 - 红色, 用户2 - 绿色等
最简单的方法是什么?我不知道如何连接这两个代码块。只是为了给你一些背景我正在做医院管理服务,我需要为每个用户分配紧急颜色。
答案 0 :(得分:2)
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
}
public class Colours
{
public string ColourName { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Person> People = new List<Person>()
{
new Person { Id = 1, Name = "Dave" },
new Person { Id = 2, Name = "Joe" },
new Person { Id = 3, Name = "Stephen"},
new Person { Id = 4, Name = "Sue" },
new Person { Id = 5, Name = "Jemma" },
new Person { Id = 6, Name = "Sharon" },
};
Dictionary<int, Colours> Colours = new Dictionary<int, Colours>()
{
{ 1, new Colours { ColourName = "Color.Red" }},
{ 2, new Colours { ColourName = "Color.Green"}},
{ 3, new Colours { ColourName = "Color.Blue"}},
};
Random RandomNumber = new Random();
int ListPostionToRemoveAt = RandomNumber.Next(1, People.Count);
int RandomColourPostion = RandomNumber.Next(1, Colours.Count);
Person p = People[ListPostionToRemoveAt];
Colours ColourToAssign = Colours[RandomColourPostion] ;
p.Color = ColourToAssign.ColourName;
Console.Write("Persons Name: " + p.Name +" Persons Colour:"+ p.Color);
Console.ReadLine();
}
}