带有直方图的C#等级数组

本文关键字:数组 直方图 | 更新日期: 2023-09-27 18:10:25

我正在瑞典学习C#的基础编程,我想知道你是否能帮助我理解一个简单的例子。

我的目标是用随机数填充数组,然后显示星号(*(或任何与随机生成的数字相同次数的符号。

这就是我的意思:

Student 1 has a grade: 4 : * * * *
Student 2 has a grade: 9 : * * * * * * * * *
etc.

这是我迄今为止想出的代码:

using System;
using System.Text;
namespace Array_1_10
{
    class Program
    {
        static void Main(string[] args)
        {
            //declar and create an int array object with 5 elements
            string tempStars = "";
            int[] grades = new int[11];
            // initiate the array using Random class methods
            Random grade = new Random();
            for (int j = 1; j < 11; j++)
                grades[j] = grade.Next(1, 9);
            //Read and display the array's elements
            for (int j = 1; j < 11; j++)
            {
                tempStars += "*" + " ";
                tempStars += "";
                Console.WriteLine("Student {0} has got: {1} : {2} ", j, grades[j], tempStars);
            }   
        }
    }
}

它填充了数组,但无论生成什么数字,星号都会从1变为10,如下所示:

Student 1 has a grade 5 : *
Student 2 has a grade 1 : * *
etc.

你能帮我做这个吗?非常感谢。Vojtech

带有直方图的C#等级数组

using System;
using System.Text;
namespace Array_1_10
{
    class Program
    {
        static void Main(string[] args)
        {
            //declar and create an int array object with 5 elements

            int[] grades = new int[11];
            // initiate the array using Random class methods
            Random grade = new Random();
            for (int j = 1; j < 11; j++)
                grades[j] = grade.Next(1, 9);
            //Read and display the array's elements
            for (int j = 1; j < 11; j++)
            {
                string tempStars = ""; //initialize string each time
                for (int lp = 0 ; lp < grades[j] ; lp++)) { // build the string
                    tempStars += "*" + " ";
                }
                Console.WriteLine("Student {0} has got: {1} : {2} ", j, grades[j], tempStars);
            }   
        }
    }
}

这里有一个更简洁的版本,它使用StringBuilder:

using System;
using System.Text;
namespace SO7626386
{
    class Program
    {
        static void Main(string[] args)
        {
            var grades = new int[11];
            // initiate the array using Random class methods
            var grade = new Random();
            for (int j = 1; j < 11; j++)
            {
                grades[j] = grade.Next(1, 10);
            }
            //Read and display the array's elements
            for (int j = 1; j < 11; j++)
            {
                Console.WriteLine("Student {0} has got: {1} : {2} ", j, grades[j], new StringBuilder().Insert(0,"* ",grades[j]).ToString());
            }   
        }
    }
}

跟踪代码的这一部分:

        for (int j = 1; j < 11; j++)
        {
            tempStars += "*" + " ";
            tempStars += "";
            Console.WriteLine("Student {0} has got: {1} : {2} ",
                                j, grades[j], tempStars);
        }

在每次迭代中,无论应该分配多少*,你都会在tempStars上添加新的星号,所以你要更改它。

例如,首先在for循环中定义tempStars,然后将*作为等级[j]的大小添加到tempStars变量中,然后打印它。

作为一个额外的解决方案,您可以自由研究:

for (int j = 1; j < 11; j++)
{
    string tempStars = new String('*', grades[j]).Replace("*", "* ");
    Console.WriteLine ....

我不会向你解释,因为在MSDN上研究它很容易。

这很有趣,因为它显示了new.之间的优先级(更清楚地说,在C#中,你不需要写这个:(new String('*', grades[j])).Replace("*", "* "),在其他语言中,你必须添加括号。(

您需要更改代码中显示oF星数的位置,以将分数考虑在内。因此,来自循环的代码将是:

For(j=1; j<11; j++)
{
    StringBuilder ab = new StringBuilder(grades[j]);
    For(int i=0; i<grades[j]; i++)
    {
        sb.Append(" *");
    }
    Console.WriteLine("Student {0} has grade {1} : {2}", j, grades[j], sb.ToString());
}

额外的for循环是用学生获得的星星数量构建一个字符串。对于这类操作,您应该使用字符串生成器,因为它比创建大量字符串效率高得多。

需要注意的一点是,代码将字符串生成器初始化为正确的字符串长度。这为字符串生成器类节省了在用于构建字符串的引擎盖下调整其数组大小的工作。

按照编写代码的方式,您只在第一个循环中打印一个星号,然后在循环的每个迭代中添加一个额外的星号。你需要一个内部循环来打印正确数量的星号,然后你需要在打印后清除外部循环中的tempStars变量。

尝试相应地更改代码中显示星号的部分:(未测试(

string tempStars = "";
for (int j = 1; j < 11; j++)
{
    for (int i = 0; i < grades[j]; i++)
        tempStars += "*" + " ";
    Console.WriteLine("Student {0} has got: {1} : {2} ", j, grades[j], tempStars);
    tempStars = "";
}

这应该更好:

using System;
using System.Text;
namespace Array_1_10
{
    class Program
    {
        string stars(int count)
        {
            if (count == 0) return "";
            string st = "*";
            for (int i = 1; i < count; i++)
            {
                st += " *";
            }
            return st;
        }
        static void Main(string[] args)
        {
            //declar and create an int array object with 10 elements
            int[] grades = new int[10];
            // initiate the array using Random class methods
            Random grade = new Random();
            for (int j = 0; j < 10; j++)
                grades[j] = grade.Next(1, 9);
            //Read and display the array's elements
            for (int j = 0; j < 10; j++)
                Console.WriteLine("Student {0} has got: {1} : {2} ", j, grades[j], stars(grades[j]));
        }
    }
}