C# 将数字替换为特定字符串

C# 将数字替换为特定字符串,c#,C#,我应该生成随机数字,然后用不同的名字给出数字 我设法创建了随机数,但我坚持给出正确的输出: 我的代码: static Random rnd = new Random(); public static int GetZufall() { int random = rnd.Next(0, 10); // from 0 to 9 return random; } static void Main(string[] args) { Console.WriteLine("

我应该生成随机数字,然后用不同的名字给出数字

我设法创建了随机数,但我坚持给出正确的输出:

我的代码:

static Random rnd = new Random();
public static int GetZufall()
{
    int random = rnd.Next(0, 10); // from 0 to 9
    return random;
}
static void Main(string[] args)
{
    Console.WriteLine("How many random numbers do you want: ");
    int input = int.Parse(Console.ReadLine());
    Console.WriteLine();
    int[] array = new int[10];
    for (int i = 0; i < input; i++)
    {
        array[GetZufall()]++;
        //Console.WriteLine(GetZufall()); // Random numbers
    }
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine($"{i} : {array[i]}");
    }
    int maxValue = array.Max(); // Linq
    Console.WriteLine();
    Console.WriteLine("Most common value: " + maxValue);
    foreach (var item in array)
    {
        string result = array.ToString();
        result = result.Replace("1", "X");
        Console.WriteLine(result);
    }
}
static Random rnd=new Random();
公共静态int GetZufall()
{
int random=rnd.Next(0,10);//从0到9
返回随机;
}
静态void Main(字符串[]参数)
{
WriteLine(“您想要多少个随机数:”);
int input=int.Parse(Console.ReadLine());
Console.WriteLine();
int[]数组=新的int[10];
对于(int i=0;i
如你所见,我尝试将数组转换为字符串,然后用X替换每个数字

例子: 输入:5个随机数 输出:

  • 0:0
  • 1:2
  • 2:0
  • 3:0
  • 4:3
  • 5:0
  • 6:0
  • 7:0
  • 8:0
  • 9:0
此输出是正确的,但下一个输出应如下所示:

foreach (int x in array)
{
    result = "";
    for (i = 0; i < x; i++)
        result += "X"
    if (result == "")
        result = "0";
}
  • 0:0
  • 1:XX
  • 2:0
  • 3:0
  • 4:XXX
  • 5:0
  • 6:0
  • 7:0
  • 8:0
  • 9:0

我还尝试过使用Regex
Regex r=new Regex(“[0-9]”,RegexOptions.None);

您正在用“X”替换1s。您应该使用这些值来添加X,如下所示:

foreach (int x in array)
{
    result = "";
    for (i = 0; i < x; i++)
        result += "X"
    if (result == "")
        result = "0";
}
foreach(数组中的int x)
{
结果=”;
对于(i=0;i
第一个错误是

string result = array.ToString(); 
应该是

string result = item.ToString();
这里有一个解决方案:

foreach (var item in array)
{
    string result = "";
    if (item == 0)
         result = "0";
    else
         for (int i = 0; i < item; i++)
             result += "X";                  
    Console.WriteLine(result);
}
foreach(数组中的变量项)
{
字符串结果=”;
如果(项==0)
结果=“0”;
其他的
对于(int i=0;i
更新


“如果我想将其输出为0:XX,以及前面的数组数,我应该怎么做?”

这应该起作用:

int count = 0;
foreach (var item in array)
{
    string result = "";
    result = count.ToString() + " : ";
    count++;
    for (int i = 0; i < item; i++)
        result += "X";
    Console.WriteLine(result);
}
int count=0;
foreach(数组中的变量项)
{
字符串结果=”;
结果=count.ToString()+“:”;
计数++;
对于(int i=0;i
“字符串结果=array.ToString();“--你想要
item.ToString
不是
array.ToString
我怀疑?也谢谢你的解决方案。如果我想输出0:XX,还有前面的数组数,我该怎么办?例如,有2:6和输出2:xxxxxx,请查看更新后的答案,让我知道是否正确。不完全正确,我已经知道了。”结果,它应该是从0到9,这将获取结果。您的解决方案:输入2:6输出是6:XXXXXX它应该是2:XXXXXX我不确定我是否完全理解。但请尝试最新更新。