講義メモ

テキスト編:p.288「DateTime構造体を使ってみる」から
ゲーム開発演習:画面遷移、タイマー処理、背景画面スクロール

p.288 DateTime構造体を使ってみる

・DateTime構造体:C#が提供する機能で、構造体として利用可能
・C/C++/Javaなどにおける日付時刻機能に近いが、最も洗練されていて使いやすい
・年月日時分秒ミリ秒を返すint型プロパティ:Year、Month、Day、Hour、Minute、Second、Millsecond。
・曜日をDayOfWeek列挙型で返すDayOfWeekプロパティもある
 これは日曜日を0とする英語曜日名の列挙子でできており、表示すると英語曜日名になる
・1年の通算日(1月1日を1日目とする日数)をint型で返すのがDayOfYearプロパティ
・現在日付時刻を持つDateTime構造体オブジェクトを返すのがNow静的プロパティ
 利用例:DateTime dt = DateTime.Now;
・DateTime構造体オブジェクトに含まれる年月日のみを(時分秒をゼロにした値)をDateTime構造体型で得るのが
 Dateプロパティ
・このDateTime構造体オブジェクトを表示すると「YYYY/MM/DD 0:00:00」形式となる。
・なお、ToShortDateString()メソッドに渡すと「YYYY/MM/DD」形式となる。
・DateTime構造体には多数のコンストラクタがあるが、年月日のみを指定する DateTime(int, int, int)や、
 年月日時分秒を指定する DateTime(int, int, int, int, int, int)が便利。

p.290 datetime01.cs

//p.290 datetime01.cs
using System;
class datetime01 {
    public static void Main() {
        DateTime dt = DateTime.Now; //現在時刻を得る
        Console.WriteLine("今日は{0}年{1}月{2}日({3})です",
            dt.Year, dt.Month, dt.Day, dt.DayOfWeek //プロパティを利用
            );
        Console.WriteLine("現在{0}時{1}分{2}秒{3}ミリセコンドです",
            dt.Hour, dt.Minute, dt.Second, dt.Millisecond); //プロパティを利用
        Console.WriteLine("dt.Date = {0}", dt.Date); //YYYY/MM/DD 0:00:00形式で表示
        Console.WriteLine("短い日付形式 = {0}", dt.ToShortDateString()); //YYYY/MM/DD 0:00:00形式で表示
    }
}

アレンジ演習:p.290 datetime01.cs

・曜日を日本語の曜日文字にしよう
・ヒント
 ・曜日名の配列を用いると良い string[] dow = {"日","月","火","水","木","金","土"};
 ・DayOfWeek構造体型からint型にキャストすると、日曜日から何日目かを示す整数になる
 ・よってこれをdow配列の添字にすると、日本語の曜日文字が得られる

作成例

//アレンジ演習:p.290 datetime01.cs
using System;
class datetime01 {
    public static void Main() {
        string[] dow = { "日", "月", "火", "水", "木", "金", "土" }; //【追加】
        DateTime dt = DateTime.Now; //現在時刻を得る
        Console.WriteLine("今日は{0}年{1}月{2}日({3})です",
            dt.Year, dt.Month, dt.Day, dow[(int)dt.DayOfWeek] //【変更】プロパティを利用
            );
        Console.WriteLine("現在{0}時{1}分{2}秒{3}ミリセコンドです",
            dt.Hour, dt.Minute, dt.Second, dt.Millisecond); //プロパティを利用
        Console.WriteLine("dt.Date = {0}", dt.Date); //YYYY/MM/DD 0:00:00形式で表示
        Console.WriteLine("短い日付形式 = {0}", dt.ToShortDateString()); //YYYY/MM/DD 0:00:00形式で表示
    }
}

アレンジ演習:p.290 datetime01.cs 続き

・コンソールから年、月、日を入力すると、曜日を表示する機能を追加しよう
・ヒント:年、月、日をDateTime構造体の、DateTime(int,int,int)コンストラクタに渡して、日付時刻オブジェクトを
 生成させて、用いると良い

作成例

//アレンジ演習:p.290 datetime01.cs
using System;
class datetime01 {
    public static void Main() {
        string[] dow = { "日", "月", "火", "水", "木", "金", "土" }; //【追加】
        DateTime dt = DateTime.Now; //現在時刻を得る
        Console.WriteLine("今日は{0}年{1}月{2}日({3})です",
            dt.Year, dt.Month, dt.Day, dow[(int)dt.DayOfWeek] //【変更】プロパティを利用
            );
        Console.WriteLine("現在{0}時{1}分{2}秒{3}ミリセコンドです",
            dt.Hour, dt.Minute, dt.Second, dt.Millisecond); //プロパティを利用
        Console.WriteLine("dt.Date = {0}", dt.Date); //YYYY/MM/DD 0:00:00形式で表示
        Console.WriteLine("短い日付形式 = {0}", dt.ToShortDateString()); //YYYY/MM/DD 0:00:00形式で表示
        Console.Write("年:"); int y = int.Parse(Console.ReadLine()); //【以下追加】
        Console.Write("月:"); int m = int.Parse(Console.ReadLine());
        Console.Write("日:"); int d = int.Parse(Console.ReadLine());
        Console.WriteLine(dow[(int)(new DateTime(y, m, d)).DayOfWeek] + "曜");
    }
}

p.291(Consoleクラスの静的プロパティとメソッド)

・Consoleクラスの静的プロパティを用いると、表示コンソールのタイトル、文字色&背景色、カーソルの有無などを操作
 できる。また、キー状態を得ることもできる。
 ・bool CursorVisible:trueにするとカーソルを表示、falseにすると非表示(動きのあるアプリケーション向き)
 ・string Title:タイトル文字列を設定
 ・ConsoleColor BackgroundColor:背景色をConsoleColor列挙子で設定
 ・ConsoleColor ForegroundColor:文字色をConsoleColor列挙子で設定
 ・bool KeyAvailable:キーが押されていたらtrueを、でなければfalseを返す
・Consoleクラスの静的メソッドを用いると、表示コンソールの大きさの変更、カーソルの移動やクリア動作などを実行できる
 ・void SetWindowSize(横幅文字数, 高さ文字数):表示コンソールの大きさを変更
 ・void SetCursorPosition(横位置, 縦位置):カーソルの位置を変更
 ・void Clear():コンソールを再描画し変更を反映する

p.291 clock01.cs

//p.291 clock01.cs
using System;
class clock01 {
    public static void Main() {
        int oldsecond = 0; //秒を比較用に保持する変数
        Console.CursorVisible = false; //カーソルを非表示に
        Console.Title = "時計"; //コンソールタイトル設定
        Console.SetWindowSize(12, 3); //コンソールの大きさ設定
        Console.BackgroundColor = ConsoleColor.Yellow; //背景色
        Console.ForegroundColor = ConsoleColor.Black; //文字色
        Console.Clear(); //変更を反映
        DateTime mt; //日付時刻オブジェクト用
        while (true) { //無限ループ
            mt = DateTime.Now; //現在日付時刻を得る
            if (mt.Second == oldsecond) { //秒が変わっていない?
                continue; //後続処理をスキップして次へ
            } else { //秒が変わっている?
                oldsecond = mt.Second; //新しい秒を取っておく
            }
            Console.SetCursorPosition(2, 1); //カーソルを前へ移動
            Console.Write("{0:00}:{1:00}:{2:00}",
                mt.Hour, mt.Minute, mt.Second); //時分秒を各2桁で表示
            if (Console.KeyAvailable) { //何かキーが押された?}
                break; //繰返しを抜ける
            }
        }
    }
}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です