第1关:逆序输出数组元素
任务描述
本关任务:编写程序,从键盘对数组的前n个数组元素依次赋值,并按照逆序的方式输出。
如:从键盘输入n的值是10,输入的数组元素数据依次是:0,1,2,3,4,5,6,7,8,9,则输出为:9,8,7,6,5,4,3,2,1,0
注意:n的值应为小于10的非负整数,否则输出input error!
相关知识
为了完成本关任务,你需要掌握:1.数组的基本概念,2.如何遍历数组。
编程要求
根据提示,在右侧编辑器补充代码。
编程提示
假设数组名为a,则数组元素的输出格式建议采用如下格式:
Console.Write("{0} ",a[i]);
测试说明
平台会对你编写的代码进行测试:
测试输入:
5
4
91
51
2
32;
预期输出:
32 2 51 91 4
测试输入:
6
5
1
151
12
22
100;
预期输出:
100 22 12 151 1 5
开始你的任务吧,祝你成功!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ch701
{
class Program
{
static void Main(string[] args)
{
/******begin*******/
int n = Convert.ToInt32(Console.ReadLine());
if (n < 0 || n > 10)
{
Console.WriteLine("input error!");
return ;
}
int[] a = new int[n];
for (int i = 0; i < n; ++i)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
Array.Reverse(a);
for (int i = 0; i < n; ++i)
{
Console.Write("{0} ", a[i]);
}
/*******end********/
}
}
}

浙公网安备 33010602011771号