编辑回顾:反转数组
本课属于 Coddy C# 学习旅程的“基础知识”单元。第 60 课,共 69 课。
挑战
简单编写一个名为 Reverse 的方法,它接收一个数字数组作为参数,并返回反转后的数组。
例如,对于 [1, 2, 3],预期输出为 [3, 2, 1]。
注意:不允许使用 Array.Reverse()。请使用循环手动实现反转。
提示:要从数组末尾访问元素,请使用索引模式 arr.Length - 1 - i。
亲手试试
using System;
public class Program {
public static int[] Reverse(int[] arr) {
// 在下面编写你的代码
}
public static void Main(string[] args) {
string text = Console.ReadLine();
string[] stringArr = text.Split(",");
int[] arr = new int[stringArr.Length];
for (int i = 0; i < stringArr.Length; i++) {
arr[i] = int.Parse(stringArr[i]);
}
int[] result = Reverse(arr);
Console.WriteLine("The reversed array is: " + string.Join(", ", result));
}
}基础知识中的所有课
自己练习: C# 在线编译器