開発環境
- 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-3を解いてみる。
練習10-3.
コード
using System; class Dog { private int weight; private string name; public Dog(int weight, string name) { this.weight = weight; this.name = name; } public int Weight { get { return weight; } set { weight = value; } } public string Name { get { return name; } set { name = value; } } } class Tester { public void Display(Dog dog) { Console.WriteLine("名前:{0}, 重さ:{1}", dog.Name, dog.Weight); } public void Run() { Dog milo = new Dog(26, "Milo"); Dog frisky = new Dog(10, "Frisky"); Dog laika = new Dog(50, "Laika"); Dog[] dogs = { milo, frisky, laika }; string[][] dogsAndAwards = new string[dogs.Length][]; dogsAndAwards[0] = new string[3]; dogsAndAwards[1] = new string[1]; dogsAndAwards[2] = new string[2]; dogsAndAwards[0][0] = "Award1"; dogsAndAwards[0][1] = "Award2"; dogsAndAwards[0][2] = "Award3"; dogsAndAwards[1][0] = "Award4"; dogsAndAwards[2][0] = "Award5"; dogsAndAwards[2][1] = "Award6"; for (int i = 0; i < dogs.Length; i++) { Display(dogs[i]); foreach (string award in dogsAndAwards[i]) { Console.WriteLine(award); } } } static void Main() { Tester t = new Tester(); t.Run(); } }
入出力結果(Console Window)
名前:Milo, 重さ:26 Award1 Award2 Award3 名前:Frisky, 重さ:10 Award4 名前:Laika, 重さ:50 Award5 Award6 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿