c# - 使用星号的图表(循环、数组)

标签 c# arrays loops charts

所以我有一个创建随机数的数组,我想将数字除以 10。所以如果数字是 45,我想在彼此下面得到 6 个“p”,并且在彼此下面有 4 个星号(让我们称之为整体东西一栏)。然后我也想要下一个数字,但我希望这些列彼此相邻而不是在下面。如果你看到图片会更容易理解。 这就是我得到的:https://gyazo.com/6e4fcf301784e0c385b9ce0d9c938b6a 这就是我想要的(这里有空格而不是'p':https://gyazo.com/778027ad74a2a3d2065348def8f431cd

这是我的代码:

    static void Main(string[] args)
    {
        int[] a = RandomFill(10);
        Print(a);

        int asd = 0;

        for (int i = 0; i <= a.Length-1; i++)
        {
            asd = a[i] / 10;

            for (int j = 0; j <= 10 - asd; j++)
            {
                Console.WriteLine("p");
            }
            for (int k = 0; k < asd; k++)
            {
                Console.WriteLine("*" + " ");
            }
            Console.WriteLine();
        }
        Console.ReadLine();


    }

    static int[] RandomFill(int Length)
    {
        Random rd = new Random();

        int[] array = new int[Length];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = rd.Next(0, 100);
        }
        return array;
    }

    static void Print(int[] a)
    {
        Console.WriteLine();
        foreach( int element in a)
        {
            Console.Write(element + " ");
        }
    }

最佳答案

似乎有效...

for (int j = 0; j < 10; j++)
            {
                string line = "";
                for (int i = 0; i <= a.Length - 1; i++)
                {
                    if (a[i] >= 10 * (10 - j))
                    {
                        line = line+"*";
                    }
                    else 
                    { 
                        line = line+"p"; 
                    }
                }
                Console.WriteLine(line);
            }

关于c# - 使用星号的图表(循环、数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53547515/

相关文章:

java - RLE压缩

database - Cakephp 循环遍历数据库保存,出于某种原因它只保存在循环的最后一个实例上?

javascript - 如何在 JavaScript 中重复一个字符串数组,让我得到它的总和?

javascript - Typescript 中用于对数组进行自定义排序的比较器

Python-循环遍历未排序的数组时计算某一项的出现次数

c# - 使用多个数据读取器

python - 如果我只切出 1 列与切出多列,为什么 numpy 的行为会有所不同?

c# - 多选枚举

c# - PCL 反射使用 BindingFlags 获取属性

c# - C# 中的基本多线程问题