【问题标题】:Assigning numbers in the list a color based on position [closed]根据位置为列表中的数字分配颜色[关闭]
【发布时间】:2018-04-04 19:36:16
【问题描述】:

所以在这里我需要一个数组来填充 1 到 100 之间的数字。 然后他们也需要被随机化。

现在我正在尝试为列表中的每个项目分配颜色、红色、黄色和白色。这是基于阵列内的位置,1=红色,2=黄色,3=白色,需要在整个阵列中重复(4=红色,5=黄色等)

我在试图找到一种方法来做到这一点时遇到了麻烦。

我研究了多维数组,但我不太确定它是否能按我需要的方式工作。我还认为也许 for 循环可以实现这一点。

或者我是否需要使用不同的枚举来为数组中的数字分配其他值。

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;
        }
    }
}

【问题讨论】:

  • 如果我理解正确,您需要用随机的红色/黄色/白色值填充 100 个元素的数组吗?你可以(作为一个非常伪代码的例子): loop array[1...100] = (Math.Random()*10) % 3
  • 你在哪里/如何分配颜色?或者你的意思是你想用某种颜色打印每个项目/
  • 你的颜色在哪里?它们与数字有什么关系?另外,您是否希望您的“随机数数组”以随机顺序排列 0-100 之间的每个数字?还是 100 个 0-100 范围内随机生成的数字?
  • 当 OP 提供的细节很少时,您到底如何将其标记为重复?我投票决定重新开放,直到提供更多细节。希望其他人会跟随
  • @PatrickArtner:我同意这不是对另一个问题的欺骗。不过,尚不清楚 OP 在问什么。可以将其视为“完成我的任务”请求。

标签: c# arrays list


【解决方案1】:

您不需要随机生成数字 - 只需生成 1 到 100 的数字并随机播放它们。这比生成数百个随机数并丢弃许多随机数要快得多(尤其是在最后你通过随机更改“绘制”很多已经存在的随机数并且不要使用它们)。

我通过生成 100 个 Guid 来使用可怜的人洗牌,获取他们的 HashCode 并订购生成的 他们的数字-我认为那是半随机的。

我通过生成映射每个生成的数字(按顺序)的字典来解决颜色映射问题 到表示为枚举值的颜色。当前枚举值通过我在每次赋值后调用的静态扩展方法进行移位。

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();
    }
}

输出:

66, Red
24, Yellow
36, White
17, Red
86, Yellow
58, White
44, Red
27, Yellow
47, White
91, Red
15, Yellow
31, White
18, Red
25, Yellow
3, White
64, Red
32, Yellow
41, White
67, Red
11, Yellow
72, White
43, Red
9, Yellow
42, White
84, Red
23, Yellow
95, White
14, Red
59, Yellow
22, White
2, Red
76, Yellow
81, White
57, Red
19, Yellow
49, White
80, Red
55, Yellow
13, White
98, Red
1, Yellow
51, White
12, Red
90, Yellow
37, White
65, Red
26, Yellow
83, White
82, Red
61, Yellow
56, White
99, Red
78, Yellow
38, White
71, Red
40, Yellow
29, White
34, Red
93, Yellow
85, White
96, Red
39, Yellow
100, White
33, Red
74, Yellow
87, White
75, Red
92, Yellow
5, White
79, Red
60, Yellow
30, White
77, Red
4, Yellow
70, White
50, Red
16, Yellow
97, White
94, Red
63, Yellow
10, White
7, Red
73, Yellow
46, White
28, Red
45, Yellow
88, White
69, Red
62, Yellow
53, White
54, Red
89, Yellow
8, White
68, Red
20, Yellow
6, White
21, Red
48, Yellow
35, White
52, Red

基本上,您有一个快速查找颜色的字典,具体取决于数字和数字数组。我会切换到列表 - 但这是偏好 - 你可能有理由使用数组。


如果您想避免显式存储颜色值,您还可以利用linq and Select 使用提供值及其索引的重载“即时”转换位置:

    // 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}");

如果您修改基本数组,第二种方法会改变颜色 - 因此,如果您需要它们保持连接,您可以使用 Dictionary 方法。


编辑在阅读了一些cmets的问题后:

如果 ypu 需要对您的数据进行后处理,您应该创建一个 POCO(结构/类)来保存:数字、位置、颜色和任何其他您需要用于以后排序/操作的属性以及对这些数据进行操作的方法 - OOP 到救援:)

【讨论】:

  • 是的,我遇到过使用字典来映射元素,但不确定是否可以使用它来映射我需要的元素。我还遇到过许多不同的情况,人们认为列表可能更合理该数组只是我选择填充随机值的第一个可枚举项。感谢您的意见,虽然帕特里克。非常感谢!我肯定会更多地在字典中查找此解决方案。
  • 不幸的是,我还不能投票给这篇文章,因为我还没有足够的代表,否则我会的。 =(
  • 我只是在查看您的代码以阅读其流程,只是对您的命名约定感到好奇。我看到您将变量命名为 aktCol 我认为 Col 用于颜色,但什么是 akt?这是某事的首字母缩写词还是不相关?
  • @JusteasyStackAttacka actualColor - 即当前应用的颜色 - 名字不好,但很好:)
  • 哦,好吧,我也是这么想的,但是 k 有点让我失望,所以我只是想我会问。
猜你喜欢
  • 2019-12-18
  • 1970-01-01
  • 1970-01-01
  • 2018-12-21
  • 1970-01-01
  • 2016-05-18
  • 2019-11-08
  • 2015-08-04
  • 2021-03-28
相关资源
最近更新 更多