为数组的值打印 X
Print X for value of 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();
}
}
}
最简单的方法可能是将 cout 语句放入 2 个 for 循环中。第一个是你要打印的数组的长度,第二个是那个地方的数字。
举个例子(array
是数组的名称,ARRAY_SIZE
是长度):
for (int x = 0; x < ARRAY_SIZE; x++) {
for (int y = 0; y <= array[x]; y++) {
output += x + " ";
}
output += "\n";
}
您要查找的内容已内置于 String class 中。它有一个构造函数
创建任意长度的重复字符字符串。不需要 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();
}
我不完全确定我是否遵循了您想要执行的操作,但这段代码可能类似于您对循环所做的操作。
使用 StringBuilder 是因为它“doesn't create a new object in the memory but dynamically expands memory to accommodate the modified string”
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);
}
}
我会按照@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();
}
我正在尝试为数组中的值打印“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();
}
}
}
最简单的方法可能是将 cout 语句放入 2 个 for 循环中。第一个是你要打印的数组的长度,第二个是那个地方的数字。
举个例子(array
是数组的名称,ARRAY_SIZE
是长度):
for (int x = 0; x < ARRAY_SIZE; x++) {
for (int y = 0; y <= array[x]; y++) {
output += x + " ";
}
output += "\n";
}
您要查找的内容已内置于 String class 中。它有一个构造函数 创建任意长度的重复字符字符串。不需要 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();
}
我不完全确定我是否遵循了您想要执行的操作,但这段代码可能类似于您对循环所做的操作。
使用 StringBuilder 是因为它“doesn't create a new object in the memory but dynamically expands memory to accommodate the modified string”
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);
}
}
我会按照@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();
}