Skip to content

Instantly share code, notes, and snippets.

@s2kw
Last active June 15, 2016 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s2kw/fe9781bf619599735b4cfde17f511490 to your computer and use it in GitHub Desktop.
Save s2kw/fe9781bf619599735b4cfde17f511490 to your computer and use it in GitHub Desktop.
毎日出題の3日目第四問、第五問の答え
public class FizzBuzz : MonoBehaviour {
// 使いまわさない文字列
static readonly string fizz = "fizz";
// 使いまわさない文字列
static readonly string buzz = "buzz";
// 3フレームい1回表示
const int printPerFrame = 3;
// fizz / buzz 用カウンタ
int counter = 0;
// 現在フレーム用カウンター
int frameCounter = printPerFrame;
void Update () {
// 300以上いなったら終了
if (counter >= 300) {
Destroy(this);
return;
}
// 3フレームに1度を判定。
if ( this.IsFrameOn3() ) return;
// 3で割ったらfizz表示
if ( counter % 3 == 0 )
{
Debug.Log( string.Format("{0}:{1}", counter, fizz) );
}else
// 5で割ったらbuzz表示
if (counter % 5 == 0 ){
Debug.Log( string.Format("{0}:{1}", counter, buzz) );
}else{
// 通常表示
Debug.Log(counter);
}
// カウンターをインクリメント
counter++;
}
bool IsFrameOn3()
{
// stringは + 加算演算子え文字列を合体できる
// Debug表示でframeの値を表示しておく。あえてwarningにすることでconsole上で色違いとして出せる。
Debug.LogWarning("frame:" + frameCounter);
//デクリメントして減らす
frameCounter--;
// フレームカウンタが0以下だったらデフォルトの値(何回に一度リセットするかを指定した値であるprintPerFrameの値)に戻す
if (frameCounter <= 0 )
{
frameCounter = printPerFrame;
}
// フレームカウンターとリセット用の数値が一緒あどうか。
return frameCounter != printPerFrame;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment