将数组中的每个值加倍

时间:2014-03-05 15:53:45

标签: c# arrays methods indexing

您好我写了一个代码,该代码应该使数组中的每个值加倍。由于未知原因,它不显示水平而是显示垂直,它将原始数字与新的double值组合,如何将它们彼此分开并水平显示。如果你能帮助我,我将非常感谢。

 int[] A = { -2, 5, -1, 9, -6, 23, 67, 1, -8, 7, -3, 90 };

 Console.WriteLine("\n=====================\n");
 Changes(A); //<<< original index value
 Console.WriteLine("\nEvery Value gets doubled");

 Changes(A); //<<< new changes

Changes()方法:

static void Changes(int[] array) //<<<<<<<< methods
{
    Console.WriteLine("\n=====================\n");
    for (int i = 0; i < array.Length; i++)
    {
        Console.WriteLine("{0}", array[i]); //<<< display orgininal value
        array[i] *= 2;
        Console.WriteLine("{0}", array[i]); // <<new value
    }
    Console.WriteLine("\n=====================\n");
}

4 个答案:

答案 0 :(得分:3)

Console.WriteLine将垂直显示输出

试试这个

        Console.Write("{0} ", array[i]); //<<< display orgininal value
        array[i] *= 2;
         //here I use = to  separate the output put you can use any other string you want 
        Console.Write(" = {0}", array[i])

答案 1 :(得分:2)

使用Console.Write()代替Console.WriteLine()

答案 2 :(得分:1)

在这种情况下,我总是习惯使用String.Join()方法。

int[] A = { -2, 5, -1, 9, -6, 23, 67, 1, -8, 7, -3, 90 };

Console.WriteLine("\n=====================\n");
Changes(A); //<<< original index value
Console.WriteLine("\nEvery Value gets doubled");

Changes(A); //<<< new changes


static void Changes(int[] array) //<<<<<<<< methods
{
    Console.WriteLine("\n=====================\n");
    Console.WriteLine("{0}", String.Join(", ", array)); //<<< display orgininal values

    for (int i = 0; i < array.Length; i++)
      array[i] *= 2;

    Console.WriteLine("{0}", String.Join(", ", array)); // <<new values
    Console.WriteLine("\n=====================\n");
}

答案 3 :(得分:-1)

试试这个:

您可以为此更改所有代码;)

int i = -1;
A.ToList().ForEach(x => Console.WriteLine(string.Format("\n=====================\nOriginal value: {0}\nNew value: {1}\n=====================\n", x, A[++i] *= 2)));

=====================

原始值:-2

新值:-4

=====================

等等......