第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********/

        }
    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM