有没有人可以帮助我?我正在写一个程序(反转数组),但它没有 运行 :(

are there anybody can help me ? i'm writting a program ( reversing an array ), but it doesn't run :(

我正在编写一个关于使用 3 种方法(GenerateNumber、reverse 和 PrintOut)反转数组的程序。

不幸的是它没有运行。你能帮我找到错误并修复它们吗?

为什么不 运行?

  public class Program
{
    static int[] GenerateNumber()
    {
        string a = Console.ReadLine();
        int b = Convert.ToInt32(a);
        int[] number = new int [b];
        string[] c = new string[b];
        for (int index = 0; index < number.Length; index++)
        {
            c[index] = Console.ReadLine();
            number [index]= Convert.ToInt32(c[index]);
       }
        return number;
    }
    static int[] reverse(int[] array)
    {

        for (int index =0; index <array.Length; index++)
        {
            int c = array[index];
            array[index] = array[array.Length - index - 1];
            array[array.Length - index - 1]= c;
        }
        return array;
    }
    static int[] PrintOut (int[] array)
    {
        for (int index = 0; index > array.Length; index++)
            Console.Write(array[index]);
        return array;
    }
           static void Main(string[] args)
    {
        int[] number = GenerateNumber();
        reverse(number);
        PrintOut(number);
        Console.ReadKey();
    }

不当行为的直接原因是

static int[] PrintOut (int[] array)
{
    for (int index = 0; index > array.Length; index++) // <- wrong condition
        Console.Write(array[index]);

比较应该是<而不是>:

    for (int index = 0; index < array.Length; index++) 

但是,更好的选择是 foreach 循环而不是 for

    foreach (var item in array)
      Console.Write(item); // propably, you want WriteLine not Write 

一些建议:

  public class Program {
    static int[] GenerateNumber() {
      // You don't want "c" array, but "number"
      int[] number = new int [Convert.ToInt32(Console.ReadLine())];

      for (int index = 0; index < number.Length; index++)  
        number [index] = Convert.ToInt32(Console.ReadLine());

      return number;
    }

    // Nice implementation, nothing to improve but naming (reverse -> Reverse)  
    static int[] Reverse(int[] array) {
        for (int index = 0; index <array.Length; index++) {
            int c = array[index];
            array[index] = array[array.Length - index - 1];
            array[array.Length - index - 1] = c;
        }
        return array;
    }

    static int[] PrintOut (int[] array) {
        // foreach is easier to implement and easier to read
        foreach (var item in array)
          Console.WriteLine(item); // <- you, probably, want WriteLine not Write 

        return array;
    }

    static void Main(string[] args) {
        int[] number = GenerateNumber();
        Reverse(number);
        PrintOut(number);
        Console.ReadKey();
    }