C# 为数组的值打印X
我正在尝试为数组中的值打印“x”,例如,整数32将打印32 x,但我不确定如何执行此操作 任何关于该做什么的帮助或指点都会很好,我们已经四处查看了,但似乎找不到任何有助于我的东西,而不会使事情变得过于复杂C# 为数组的值打印X,c#,arrays,console-application,histogram,C#,Arrays,Console Application,Histogram,我正在尝试为数组中的值打印“x”,例如,整数32将打印32 x,但我不确定如何执行此操作 任何关于该做什么的帮助或指点都会很好,我们已经四处查看了,但似乎找不到任何有助于我的东西,而不会使事情变得过于复杂 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namesp
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
名称空间直方图
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输出=”;
int[]x;
x=新整数[18];
int[]y={32,27,64,18,95,14,90,70,60,37,17,56,99,34,75,36,12,8100,77};
常量int数组_SIZE=18;
int[]z;
z=新整数[数组大小];
对于(int i=0;i
最简单的方法可能是将cout语句放入2个for循环中。第一个表示要打印的数组长度,第二个表示该位置的数字
例如(array
是数组的名称,array\u SIZE
是长度):
for(int x=0;x for(int y=0;y您要查找的内容已经内置到String类中。它有一个构造函数
创建任意长度的重复字符字符串。不需要字符串生成器或任何额外的循环,这将过于复杂
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();
}
static void Main(字符串[]args)
{
字符串输出=”;
常量int数组_SIZE=18;
int[]x=新的int[ARRAY_SIZE];
int[]z=新的int[ARRAY_SIZE];
int[]y={32,27,64,18,95,14,90,70,60,37,17,56,99,34,75,36,12,8100,77};
对于(int i=0;i
我不完全确定我是否遵循了您想要执行的操作,但这段代码可能与您使用循环执行的操作类似
使用StringBuilder,因为它为“”
使用制度;
使用系统文本;
公共课程
{
公共静态void Main()
{
字符串输出=”;
StringBuilder sb=新StringBuilder(“x”);
int[]y={32,27,64,18,95,14,90,70,60,37,17,56,99,34,75,36,12,8100,77};
//用于确定y数组中要经过的元素数
常量int数组_SIZE=5;
int[]z;
z=新整数[数组大小];
对于(int i=0;i
我会使用@TaW建议的PadRight
方法。我看不出您将结果存储在输出变量上,然后打印它的任何原因
请在下面找到我的解决方案(删除了不必要的代码),我可以立即打印,无需在内存中存储任何内容:
static void Main(string[] args)
{
const char paddingChar = 'X';
const string tabs = "\t\t";
int[] y = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37, 17, 56, 99, 34, 75, 36, 12, 8, 100, 77 };
Console.WriteLine($"Element{tabs}Value{tabs}Histogram");
for (int i = 0; i < y.Length; i++)
{
Console.WriteLine($"{i}{tabs}{y[i]}{tabs}{"".PadRight(y[i], paddingChar)}");
}
Console.ReadKey();
}
static void Main(字符串[]args)
{
常量字符paddingChar='X';
常量字符串制表符=“\t\t”;
int[]y={32,27,64,18,95,14,90,70,60,37,17,56,99,34,75,36,12,8100,77};
WriteLine($“元素{tabs}值{tabs}直方图”);
对于(int i=0;i
要用n个字符填充或附加字符串,请使用变量x
和z
的用法,它们从未在您的代码中使用过?感谢链接,我将查看一下,看看是否可以计算出来。x用于打印y值在so元素1=32内的元素,以及需要删除的z值It实际上@NazimHmm很有趣,我不确定这一点,但我会试试看它是如何工作的。我不确定你是否遵循了我正在尝试做的。对于数组中存储的值,我想为值32打印一个x,因此对于值32,将打印32个x。我知道了,我现在将尝试实现它,看看它是如何运行的。好的,我再次编辑了它,试试看。我想这就是你想要的。是的,这正是我想要做的。
using System;
using System.Text;
public class Program
{
public static void Main()
{
string output = "";
StringBuilder sb = new StringBuilder("x");
int[] y = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37, 17, 56, 99, 34, 75, 36, 12, 8, 100, 77 };
//used to determine how many elements in the y array to go through
const int ARRAY_SIZE = 5;
int[] z;
z = new int[ARRAY_SIZE];
for(int i=0; i< z.Length; i++){
if(sb.Length != y[i])
sb.Clear();
for(int j=0; j < y[i]; j++)
sb.Append("x");
output += i + "\t\t" + y[i] + "\t\t" + sb + "\t\t" + "\n";
//clear the string builder var for next set of x's
sb.Clear();
}
Console.WriteLine(output);
}
}
static void Main(string[] args)
{
const char paddingChar = 'X';
const string tabs = "\t\t";
int[] y = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37, 17, 56, 99, 34, 75, 36, 12, 8, 100, 77 };
Console.WriteLine($"Element{tabs}Value{tabs}Histogram");
for (int i = 0; i < y.Length; i++)
{
Console.WriteLine($"{i}{tabs}{y[i]}{tabs}{"".PadRight(y[i], paddingChar)}");
}
Console.ReadKey();
}