c# - 打印 X 以获取 Array 的值
问题描述
我正在尝试为数组中的值打印“x”,例如整数 32 将打印 32 个 x,但我不知道该怎么做。
任何关于做什么的帮助或指示都会很好,但似乎找不到任何对我有帮助的东西而不会使它过于复杂。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Histogram
{
class Program
{
static void Main(string[] args)
{
string output = "";
int[] x;
x = new int[18];
int[] y = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37, 17, 56, 99, 34, 75, 36, 12, 8, 100, 77 };
const int ARRAY_SIZE = 18;
int[] z;
z = new int[ARRAY_SIZE];
for (int i = 0; i < z.Length; i++)
z[i] = 2 * i;
Console.WriteLine("Element\t \tValue\t \tHistogram\t\t\n");
for (int i = 0; i < ARRAY_SIZE; i++)
{
output += i + "\t\t" + y[i] + "\t\t" + y[i] + "\t\t" + "\n";
}
Console.WriteLine(output);
Console.ReadKey();
}
}
}
解决方案
您正在寻找的内容已经内置到 String 类中。它有一个构造函数来创建一串任意长度的重复字符。不需要 String Builder 或任何额外的循环,这会过于复杂。
static void Main(string[] args)
{
string output = "";
const int ARRAY_SIZE = 18;
int[] x = new int[ARRAY_SIZE];
int[] z = new int[ARRAY_SIZE];
int[] y = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37, 17, 56, 99, 34, 75, 36, 12, 8, 100, 77 };
for (int i = 0; i < z.Length; i++)
z[i] = 2 * i;
Console.WriteLine("Element\t \tValue\t \tHistogram\t\t\n");
for (int i = 0; i < ARRAY_SIZE; i++)
{
string bar = new string('X', y[i]);
output += i + "\t\t" + y[i] + "\t\t" + bar + "\t\t" + "\n";
}
Console.WriteLine(output);
Console.ReadKey();
}
推荐阅读
- elasticsearch - 带有大写标记的 Elasticsearch word_delimiter 过滤器不匹配
- discord - discord.js 尝试使用 2 个数组创建随机结果命令
- php - 如何使用 INNER JOIN 改进我的 php sub_array 代码以在 DataTables 上显示数据?
- sql - 如何在sql中创建一个可以对当前日期和我的表中的值进行操作的函数
- webgl - Webgl2 maximum size of 3D texture?
- json - 邮递员如何在邮递员中解析具有动态键的嵌套 json 对象?
- dart - Flutter:在多个列表或列之间排序、拖放
- postgresql - 运行 mix ecto.setup 命令时连接被拒绝,使用 asdf 安装的软件包
- java - 如何使用按钮更改背景颜色?
- python - 预测用户评分