2012年6月2日土曜日

開発環境

  • Microsoft Windows 7 Home Premium (OS)
  • Microsoft Visual C# 2010 Express Edition (IDE)
  • 言語: C#

『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-7)の 第10章(配列)10.9(練習問題)、練習10-2を解いてみる。

練習10-2.

コード

using System;

namespace Sample
{
    class Tester
    {
        public void Display(string description, int[] intArray)
        {
            Console.Write("{0}: ", description);
            foreach (int item in intArray)
            {
                Console.Write("{0} ", item);
            }
            Console.WriteLine();
        }
        public void Run()
        {
            int[] intArray = new int[10];
            for (int i = 0; i < 10; i++)
            {
                intArray[i] = Convert.ToInt16(Console.ReadLine());
            }
            Array.Sort(intArray);
            Display("昇順", intArray);
            Array.Reverse(intArray);
            Display("降順", intArray);
        }
        static void Main()
        {
            Tester t = new Tester();
            t.Run();
        }
    }
}

入出力結果(Console Window)

1
0
2
9
3
8
4
7
5
6
昇順: 0 1 2 3 4 5 6 7 8 9
降順: 9 8 7 6 5 4 3 2 1 0
続行するには何かキーを押してください . . .

0 コメント:

コメントを投稿