開発環境
- 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;
namespace Sample
{
public class Dog
{
private int weight;
private string name;
public int Weight
{
get { return weight; }
set { weight = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public Dog(int weight, string name)
{
this.weight = weight;
this.name = name;
}
public void Display()
{
Console.WriteLine("重さ:{0}, 名前:{1}",
weight, name);
}
}
class Tester
{
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[][] awards = new string[dogs.Length][];
awards[0] = new string[3] { "award1", "award2", "award3" };
awards[1] = new string[1] { "award4"};
awards[2] = new string[2] { "award5", "award6" };
int i = 0;
foreach (Dog dog in dogs)
{
dog.Display();
Console.Write("受賞した賞: ");
foreach (string award in awards[i])
{
Console.Write("{0} ", award);
}
Console.WriteLine();
i++;
}
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
}
入出力結果(Console Window)
重さ:26, 名前:Milo 受賞した賞: award1 award2 award3 重さ:10, 名前:Frisky 受賞した賞: award4 重さ:50, 名前:Laika 受賞した賞: award5 award6 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿