使用 inputData 反向列表

问题描述

我想用 console.ReadLine() 反转数组,但我卡住了。
这是问题说明。

给出一个由 10 个自然数组成的数组。以相反的顺序显示字符串中的数字。

字符串中的数字在一行中给出,并用空格分隔。应用程序将在一行中显示数字,并在它们之间用空格分隔。

示例:

对于输入数据:

1 2 3 4 5 6 7 8 9 10

控制台将显示

10 9 8 7 6 5 4 3 2 1

这是我的代码

string number = Convert.ToString(Console.ReadLine());

List<string> originalList = new List<string>();

for (int i = 0; i < 1; i++)
{
    originalList.Add(number);
}
foreach (string item in originalList)
{
    Console.Write(item + " ");
}
originalList.Reverse();

foreach (string item in originalList)
{
    Console.WriteLine(item + " ");
}
Console.WriteLine();

解决方法

字符串中的数字在一行中给出,并用空格分隔。

下面的代码是错误的,因为Console.ReadLine在遇到空格时并没有停止读取。它会将整个输入作为 originalList 中的一个条目使用。

string number = Convert.ToString(Console.ReadLine());

List<string> originalList = new List<string>();

for (int i = 0; i < 1; i++)
{
    originalList.Add(number);
}

// originalList is now [ "1 2 3..." ];

相反,您应该查看 String.Split

string input = Console.ReadLine();

List<string> originalList = input.Split(' ').ToList();

// originalList is now [ "1","2","3",... ];

您现在可以使用 List.ReverseString.Join 来获取输出。

originalList.Reverse();

string output = string.Join(' ',originalList);

Console.WriteLine(output);
,

需要 string.Split (' ')for 循环或 while 循环

使用 string.Split (' ')

string [] splitArray = number.Split (' ');
for (int i = 0; i < splitArray.Length; i++)
{
    originalList.Add(splitArray [i]);
}
//rest of the code would be same

For 循环

String readvalues="";
for (int i=0;i <number.Length;i++)
{
    if (i==number Length-1)
    {
        readvalues += number [i];
    }
    if (number[i]==" " || i==number.Length-1)
    {
        originalList.Add (readvalues);
        readvalues="";
    }
    else
    {
        readvalues += number [i];
    }
}
//rest of the code would be same

While 循环

int index=0;
String readvalues="";
while (index < number.Length)
{
    if (index==number.Length-1)
    {
        readvalues += number [index];
    }
    if (number[index]==" " ||index==number.Length-1)
    {
        originalList.Add (readvalues);
        readvalues="";
    }
    else
    {
        readvalues += number [index];
    }
    index++;
}
//rest of the code would be same
,

您可以尝试在 Linq (Reverse) 的帮助下查询初始字符串:

代码:

  using System.Linq;

  ...

  // For Test (comment it out for release)
  string source = "1 2 3 4 5 6 7 8 9 10";

  // Uncomment for release 
  // string source = Console.ReadLine();

  string result = string.Join(" ",source
    .Split(' ',StringSplitOptions.RemoveEmptyEntries)
    .Reverse());

  Console.WriteLine(result);

结果:

  10 9 8 7 6 5 4 3 2 1
,

我想,我有一个解决方案,你到底要什么。请检查代码并告诉我=>

void Test()
{
        string number = Convert.ToString(Console.ReadLine());
        List<string> originalList = new List<string>();

        if(!string.IsNullOrEmpty(number) && !string.IsNullOrWhiteSpace(number) )
        {
            originalList=number.Split(" ").ToList();
            originalList.Reverse();
            Console.WriteLine(string.Join(" ",originalList));
        } 
}

注意:使用这三个命名空间using System;using System.Linq;using System.Collections.Generic;