計算機科学のブログ

namespaces and classes - Organizing your code - keep track of the values

Head First C Sharp: A Learner’s Guide to Real-World Programming with C Sharp and .NET (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)の Chapter 3(namespaces and classes - Organizing your code)、p.185(Sharpen your pencil)の解答を求めてみる。

コード

Program.cs

using ConsoleApp4;

Pizzazz foxtrot = new Pizzazz() { Zippo = 2 };
foxtrot.Bamboo(foxtrot.Zippo);
// foxtrot.Zipp: 4

Pizzazz november = new Pizzazz() { Zippo = 3 };
Abracadabra tango = new Abracadabra() { Vavavoom = 4 };

// 1回目
// tango.Lala(3)
// 3 < 4: true
// tango.Vavavoom: 7
// return true
// november.Zippo: -3
// november.Zippo: 4
// foxtrot.Zippo: 8
// tango.Vavavoom: -1

// 2回目
// tango.Lala(4)
// 4 < -1
// false

while (tango.Lala(november.Zippo))
{
    november.Zippo *= -1;
    november.Bamboo(tango.Vavavoom);
    foxtrot.Bamboo(november.Zippo);
    tango.Vavavoom -= foxtrot.Zippo;
}

bool[] bools = [november.Zippo == 4, foxtrot.Zippo == 8, tango.Vavavoom == -1];
foreach (bool item in bools)
{
    Console.WriteLine(item);
}

入出力結果(Terminal, Zsh)

True
True
True