c# - 如何根据数组的值打印星星?

标签 c#

我最近编写了一个程序来计算给定数组的总和和平均值。该程序还想打印与索引值匹配的直方图或星形模式。

像这样:

arraySS

这是我到目前为止写的代码:

private void process_Click(object sender, EventArgs e)
{
    string temp2 = null;
    string temp3 = null;
    float sum=0;
    int countingNumber = 1;
    string message;
    int count = Convert.ToInt32(inputArray.Text);
    int[] varray = new int[count];
    for (int i = 0; i < count; i++)
        //for (int j = 1; j <= count; j++)
        {           
            varray[i] = Convert.ToInt32(Interaction.InputBox(message = "enter the value of array number " + countingNumber));
            sum = sum+ varray[i];
            temp += countingNumber + "           "+varray[i] + Environment.NewLine;
            temp2 += countingNumber + "                " + varray[i] + "             *" + Environment.NewLine;
            box1.Text = Convert.ToString("Index     Value" + Environment.NewLine + temp);
            boxSum.Text = Convert.ToString(sum);
            boxAvg.Text = Convert.ToString(sum/count);
            countingNumber++;
        }
        for (int stars = 0; stars <= i; stars++)
        {
            temp3 = " ";
            box2.Text = Convert.ToString("Element     Value    Histogram" + Environment.NewLine + temp2+temp3);
        }
    }
}

我的代码不会打印与值匹配的星星。谁能帮帮我?

最佳答案

尝试替换这一行:

temp2 += countingNumber + "                " + varray[i] + "             *" + Environment.NewLine;

用这一行:

temp2 += countingNumber + "                " + varray[i] + "          " + new String('*', i) + Environment.NewLine;

关于c# - 如何根据数组的值打印星星?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57982817/

相关文章:

c# - SetSupportActionBar 在 Visual Studio 和 Xamarin 中崩溃

c# - 无法访问 asp.net 中对应的 .cs 文件

c# - 如何使用 MySQL 查询将组合框的所有项目发送到 SQL DB

c# - 如何读取 Excel 电子表格中的单个列?

c# - 移动数字范围的中点

c# - 将 curl 与 Phalanger 结合使用

c# - 从 YouTube 下载视频

c# - 在 GridView 数据集中格式化 DateTime 字符串表示

c# - 不使用设计器的 .NET 类图

c# - 以 C# 形式将文本框中的信息显示到列表框中