2018年4月27日金曜日

開発環境

Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の8章(例外処理 - 消火は時代遅れ)、プールパズル(p. 359)を取り組んでみる。

コード

using System;
using System.IO;

class Program
{
    static void Main()
    {
        Kangaroo Joey = new Kangaroo();
        int Koala = Joey.Wombat(
            Joey.Wombat(
                Joey.Wombat(1)));
        try
        {
            Console.WriteLine((15 / Koala) + "個の卵");
        }
        catch (DivideByZeroException)
        {
            Console.WriteLine("こんにちは!");
        }
    }
}
class Kangaroo
{
    FileStream fs;
    int Croc;
    int Dingo = 0;

    public int Wombat(int Wallaby)
    {
        Dingo++;
        try
        {
            if (Wallaby > 0)
            {
                fs = File.OpenWrite("wobbiegong");
                Croc = 0;
            }
            else if (Wallaby < 0)
            {
                Croc = 3;
            }
            else
            {
                fs = File.OpenRead("wobbiegong");
                Croc = 1;
            }
        }
        catch (IOException)
        {
            Croc = -3;
        }
        catch
        {
            Croc = 4;
        }
        finally
        {
            if (Dingo > 2)
            {
                Croc -= Dingo;
            }
        }
        return Croc;
    }
}

入出力結果(コマンドプロンプト)

こんにちは!
続行するには何かキーを押してください . . .

0 コメント:

コメントを投稿