C# 根据位置为列表中的数字指定颜色
所以这里我需要一个数组来填充1到100之间的数字。 然后,他们也需要随机化 现在,我尝试为列表中的每个项目分配一种颜色,红色、黄色和白色。这是基于1=红色、2=黄色、3=白色的阵列中的位置,需要在整个阵列中重复(4=红色、5=黄色等) 我很难找到这样做的方法 我研究了多维数组,但我不太确定它是否能按我需要的方式工作。我还认为也许for循环可以实现这一点 或者我是否需要使用不同的枚举来为数组中的数字分配其他值C# 根据位置为列表中的数字指定颜色,c#,arrays,list,C#,Arrays,List,所以这里我需要一个数组来填充1到100之间的数字。 然后,他们也需要随机化 现在,我尝试为列表中的每个项目分配一种颜色,红色、黄色和白色。这是基于1=红色、2=黄色、3=白色的阵列中的位置,需要在整个阵列中重复(4=红色、5=黄色等) 我很难找到这样做的方法 我研究了多维数组,但我不太确定它是否能按我需要的方式工作。我还认为也许for循环可以实现这一点 或者我是否需要使用不同的枚举来为数组中的数字分配其他值 class Program { static void Main(string[
class Program
{
static void Main(string[] args)
{
int[] x = new int[101];
Random r = new Random();
int i;
for (i = 0; i < x.Length; i++)
{
var next = 0;
while (true)
{
next = r.Next(101);
if (!Contains(x, next)) break;
}
x[i] = next;
Console.WriteLine("x[{0}] = {1}", i, x[i]);
}
Console.ReadLine();
}
static bool Contains(int[] array, int value)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] == value) return true;
}
return false;
}
}
}
类程序
{
静态void Main(字符串[]参数)
{
int[]x=新的int[101];
随机r=新随机();
int i;
对于(i=0;i
您不需要生成随机数-只需生成1到100的数字并将其洗牌即可。这比生成成百上千个随机数并扔掉许多随机数要快得多(尤其是在最后,您通过随机更改“绘制”了许多已经存在的随机数,并且不使用它们)
我使用穷人洗牌,生成100个guid,获取它们的HashCode并对生成的guid进行排序
我认为这是半随机的
我通过生成一个字典(按顺序)映射每个生成的数字来解决颜色映射问题
到表示为枚举值的颜色。当前枚举值在每次赋值后使用我调用的静态扩展方法进行移位
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
public enum Col { Red, Yellow, White }
public static class ColExtension
{
// Poor mans wrap around iterator for this enum: Red -> Yellow -> White -> Red ...
public static Col Next(this Col col)
{
if (col == Col.Red)
return Col.Yellow;
if (col == Col.Yellow)
return Col.White;
return Col.Red;
}
}
internal class Program
{
public static void Main(string[] args)
{
// create numbers ranging from 1 to 100. Pseudo-randomize order by ordering
// after randomized generated value
var numbers = Enumerable
.Range(1, 100).OrderBy(n => Guid.NewGuid().GetHashCode())
.ToArray();
// start-color for the first one
var aktCol = Col.Red;
var mapping = new Dictionary<int, Col>();
foreach (var number in numbers)
{
// assign first color
mapping[number] = aktCol;
// advance color
aktCol = aktCol.Next();
}
foreach (var num in numbers)
Console.WriteLine($"{num}, {mapping[num]}");
Console.ReadLine();
}
}
基本上,你有一个快速查找字典的颜色取决于数字和数组的数字。我会切换到列表-但这是首选-您可能有理由使用数组
如果要避免显式存储颜色值,还可以利用提供值及其索引的重载“动态”转换位置:
// local converter function - %3 gets the rest of div by 3 so 0=red, 1=yellow,...
// the %3 makes it wrap around and the enum parse converts it to the enum
Col ByIndexPos(int pos) => (Col)Enum.Parse(typeof(Col), $"{pos % 3}");
// On the fly color creation from index position:
foreach (var anon in numbers.Select( (v, pos) =>
new { Color = ByIndexPos(pos), Value = v })
)
Console.WriteLine($"{anon.Value}, {anon.Color}");
但是,如果修改基本数组,第二种方法将改变颜色,因此如果需要它们保持连接,可以使用字典方法
阅读部分问题评论后进行编辑:
如果ypu需要对您的数据进行后处理,您应该创建一个POCO(结构/类)来保存:编号、位置、颜色和以后排序/操作所需的任何其他属性,以及对该数据进行操作的方法-OOP to the rescue:)如果我理解正确,您需要用随机的红/黄/白值填充100个元素的数组吗?您可以(作为一个非常伪代码的示例):循环数组[1…100]=(Math.Random()*10)%3在何处/如何分配颜色?或者你的意思是你想用某种颜色打印每个项目/你的颜色在哪里以及它们与数字的关系如何?另外,您是否希望“随机数数组”中的每个数字都以随机顺序从0到100?或者100个随机生成的数字,范围在0-100之间?在OP提供的细节很少的情况下,你怎么能标记为重复的?我投票决定重新开放,直到提供更多细节。希望其他人会follow@PatrickArtner当前位置我同意这不是对另一个问题的愚弄。不过,尚不清楚OP在问什么。你可以把这看作是一个“完成我的作业”的请求。是的,我遇到过使用字典映射元素的情况,但不确定是否可以使用它映射我需要的元素。我也遇到过很多不同的例子,人们认为列表可能更合理。我只是没有看到很多方法在列表中随机分配数字,而不定义每个列表项,但我相信有一种方法可以做到这一点。该数组只是我选择填充随机值的第一个可枚举项。谢谢你的意见,帕特里克。非常感谢!对于这个解决方案,我肯定会在字典中查找更多信息。很遗憾,我还不能更新这篇文章,因为我还没有足够的代表,否则我会=(我刚刚查看了您的代码以了解其流程,对您的命名约定很好奇。我看到您将变量命名为aktCol,我认为Col代表颜色,但什么是akt?这是某事物的首字母缩略词还是与之无关?@JusteasyStackAttacka actualColor-ie-当前应用的名称-一个坏名称,但很好:)哦,好的,是的,我也这么想,但是k有点通过我,所以我想我会问。
// local converter function - %3 gets the rest of div by 3 so 0=red, 1=yellow,...
// the %3 makes it wrap around and the enum parse converts it to the enum
Col ByIndexPos(int pos) => (Col)Enum.Parse(typeof(Col), $"{pos % 3}");
// On the fly color creation from index position:
foreach (var anon in numbers.Select( (v, pos) =>
new { Color = ByIndexPos(pos), Value = v })
)
Console.WriteLine($"{anon.Value}, {anon.Color}");