LoginSignup
28
12

More than 3 years have passed since last update.

C#&Unityの演習問題:しまづ君にプログラミングを教えよう!パート1【スタジオしまづアカデミア】

Last updated at Posted at 2020-02-26

パート4まであります

(ベータ版:いいねと思ったらtwitterに投稿してもらえると嬉しいです^^)
(おすすめの問題があれば、編集リクエストで追加していただけると助かります)

これはあなた自身の物語です

ときは戦国、、、
ゲーム開発をするためにプログラミングを学び始めたしまづ君(永遠の3才)。
ところが彼はまだまだプログラミングのことがわかっていません。
のちに世界を救うこととなる「スタジオしまづ」設立のために彼にプログラミングを教えましょう!

演習問題を行う前の準備:困った人向け

スタジオしまづのYouTube
スタジオしまづの学習サイト

演習問題:変数の宣言方法がわかっている?

しまづくん:コードを書いていたらこんなエラーが出てしまったよ。。。
どうすればいいかな?
スクリーンショット 2020-02-26 9.19.55.png

しまづ君のコード

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    myStudio = "スタジオしまづ";
    void Start()
    {
        Debug.Log(myStudio);
    }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    string myStudio = "スタジオしまづ";
    void Start()
    {
        Debug.Log(myStudio);
    }
}

しまづ君:ありがとう!変数の宣言方法がわかったよ^^

演習問題:変数の種類(データ型)がわかっているか

しまづくん:僕のプロフィールをUnityさんに教えようとしたら、こんなエラーが出てしまったよ。。。
どうすればいいかな?
スクリーンショット 2020-02-26 9.40.19.png

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    // 名前:スタジオしまづ
    // 身長:163.0cm
    // 体重:44.5kg
    // 誕生日:5月7日
    bool myName = "スタジオしまづ";
    float height = 163.0;
    int weight = 44.5;
    string monthOfbirth = 5;
    Vector3 dayOfbirth = 7;

    void Start()
    {
        Debug.Log("名前:" + myName);
        Debug.Log("身長:" + height);
        Debug.Log("体重:" + weight);
        Debug.Log("誕生日:" + monthOfbirth+"月"+ dayOfbirth + "日");
    }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    // 名前:スタジオしまづ
    // 身長:163.0cm
    // 体重:44.5kg
    // 誕生日:5月7日
    string myName = "スタジオしまづ";
    float height = 163.0f;
    float weight = 44.5f;
    int monthOfbirth = 5;
    int dayOfbirth = 7;

    void Start()
    {
        Debug.Log("名前:" + myName);
        Debug.Log("身長:" + height);
        Debug.Log("体重:" + weight);
        Debug.Log("誕生日:" + monthOfbirth+"月"+ dayOfbirth + "日");
    }
}

しまづ君:ありがとう!これで誕生日プレゼントがみんなからたくさんもらえるぞ^^

演習問題:変数の四則演算子がわかっているか

しまづくん:123かける456割る2を計算してコンソールに表示したいんだけどどうかけばいいかな?

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    // 123かける456割る2の計算結果をコンソールに表示したい
    void Start()
    {
    }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    // 123かける456割る2の計算結果をコンソールに表示したい
    void Start()
    {
        Debug.Log(123*456/2); // 28044
    }
}

しまづ君:ありがとう!算数も勝手にやってくれるんだね^^

演習問題:変数の四則演算子がわかっているか

しまづくん:1+2は3でしょ?そこに5をかけると15じゃん!でも11が出るんだけど、、、
どうすれば15になるかなぁ?

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int x = 1;
    int y = 2;
    int z = 5;
    // 1+2をおこなってから5をかけて、コンソールに15を表示したい
    void Start()
    {
        Debug.Log(x+y*z);
    }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int x = 1;
    int y = 2;
    int z = 5;
    // 1+2をおこなってから5をかけて、コンソールに15を表示したい
    void Start()
    {
        Debug.Log((x+y)*z);
    }
}

しまづ君:教えてくれてありがとう!掛け算が先に計算されちゃうんだ^^

演習問題:変数の四則演算子がわかっているか

しまづくん:食事会の費用19800円を7人で割り勘することになったんだけど、割り切れなかった分はパパが払ってくれるんだ。
1人あたり何円集めて、いくらパパから貰えばいいかな?

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int x = 19800;
    int n = 7;
    // x,y,zを3で割ったあまりをそれぞれ表示したい
    void Start()
    {
        int y = 0; // ここに計算をかく
        int z = 0; // ここに計算をかく
        Debug.Log("1人あたりからもらうお金:"+ y);
        Debug.Log("パパからもらうお金:"+z);
    }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int x = 19800;
    int n = 7;
    // x,y,zを3で割ったあまりをそれぞれ表示したい
    void Start()
    {
        int y = x / n; // ここに計算をかく
        int z = x % n; // ここに計算をかく
        Debug.Log("1人あたりからもらうお金:"+ y);
        Debug.Log("パパからもらうお金:"+z);
    }
}

しまづ君:教えてくれてありがとう!パパのところに行ってこよ^^

演習問題:変数の演算子がわかっているか

しまづくん:1つの変数だけで、数字を1づつ増やして表示したいんだけどどうすればいいかな?
できれば++とか+=とかを使いたい(> <)

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int x = 0;
    void Start()
    {
        // 以下をxのみを使って行いたい(++, += を使ってやってみて)
        Debug.Log(x);
        int x1 = x + 1;
        Debug.Log(x1);
        int x2 = x1 + 1;
        Debug.Log(x2);
        int x3 = x2 + 1;
        Debug.Log(x3);
    }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int x = 0;
    void Start()
    {
        // 以下をxのみを使って行いたい(++, += を使ってやってみて)
        Debug.Log(x);
        x = x + 1;
        Debug.Log(x);
        x += 1;
        Debug.Log(x);
        x ++;
        Debug.Log(x);
    }
}

しまづ君:教えてくれてありがとう!なんかプログラマになれた気分^^

演習問題:比較演算子(イコール)がわかっているか

しまづくん:10と2が同じ数字か調べてみたら、2が返ってきたんだよ!!!!
どこが違うと思う?

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int n = 10;
    int m = 2;
    void Start()
    {
        Debug.Log(n = m);
    }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int n = 10;
    int m = 2;
    void Start()
    {
        Debug.Log(n == m);
    }
}

しまづ君:教えてくれてありがとう!プログラムの=は数学の=と違うんだね^^

演習問題:比較演算子(否定)がわかっているか

しまづくん:10と2が違う数字か調べてみたら、エラーが出ちゃった!!!!
どこが違うと思う?

スクリーンショット 2020-02-27 11.14.32.png

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int n = 10;
    int m = 2;
    void Start()
    {
        Debug.Log(n =! m);
    }
}


---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int n = 10;
    int m = 2;
    void Start()
    {
        Debug.Log(n != m);
    }
}

しまづ君:教えてくれてありがとう! 「!」の位置に決まりがあるんだね^^

演習問題:ifがわかっているか

しまづくん:HPが0になったら「戦闘不能」と表示したかったんだけど、ずっと出ちゃってる。。。
どうすればいいかな?

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int hp = 100;
    void Start()
    {
        if (hp >= 0)
        {
            Debug.Log("戦闘不能");
        }
    }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int hp = 100;
    void Start()
    {
        if (hp <= 0)
        {
            Debug.Log("戦闘不能");
        }
    }
}

しまづ君:教えてくれてありがとう! この辺りよくわ違えちゃうかも><

演習問題:if elseがわかっているか

しまづくん:HPが0になったら「戦闘不能」、それ以外のときは「しまちゅー元気でちゅー」と表示したかったんだけど、ずっと元気なんだw
どうすればいいかな?

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int hp = 100;
    void Start()
    {
        if (hp <= 0)
        {
            Debug.Log("戦闘不能");
        }
        Debug.Log("しまちゅー元気でちゅー");
    }
}


---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int hp = 100;
    void Start()
    {
        if (hp <= 0)
        {
            Debug.Log("戦闘不能");
        }
        else
        {
            Debug.Log("しまちゅー元気でちゅー");
        }
    }
}

しまづ君:教えてくれてありがとう! elseを使えばいいんだね

演習問題:if-elseifがわかっているか

しまづくん:x>0なら「右に動く」、x<0なら「左に動く」、それ以外なら「そのまま」っと表示したいんだけど、「左に動く」と「そのまま」が出ちゃうんだ。。。
どうすればいい?

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    //int x = 1; // x > 0のときのテスト用
    //int x = 0; // xが0のときのテスト用
    int x = -1;  // x < 0のときのテスト用
    void Start()
    {
        if (x > 0)
        {
            Debug.Log("右に動く");
        }
        else
        {
            Debug.Log("左に動く");
        }
        Debug.Log("そのまま");
    }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    //int x = 1; // x > 0のときのテスト用
    //int x = 0; // xが0のときのテスト用
    int x = -1;  // x < 0のときのテスト用
    void Start()
    {
        if (x > 0)
        {
            Debug.Log("右に動く");
        }
        else if(x < 0)
        {
            Debug.Log("左に動く");
        }
        else
        {
            Debug.Log("そのまま");
        }
    }
}

しまづ君:教えてくれてありがとう! else ifを使えば色々分けられるんだ^^

演習問題:switchがわかっているか

しまづくん:以下のようなプログラムを作ろうとしてるんだけど、、、
・directionが1ならspeedは1.1
・directionが0ならspeedは0
・directionが-1ならspeedは-1.1
ネットで見つけた記事からここまでは書けたんだけど、あとどうすればいいかな?

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int direction = 1;   // direction > 0のときのテスト用
    //int direction = 0;   // directionが0のときのテスト用
    //int direction = -1;   // direction < 0のときのテスト用
    float speed;

    void Start()
    {
        switch (direction)
        {
            case 100:
                speed = 22f;
                break;
            case -10:
                speed = -22f;
                break;
        }
    }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int direction = 1;   // direction > 0のときのテスト用
    //int direction = 0;   // directionが0のときのテスト用
    //int direction = -1;   // direction < 0のときのテスト用
    float speed;

    void Start()
    {
        switch (direction)
        {
            case 1:
                speed = 1.1f;
                break;
            case 0:
                speed = 0f;
                break;
            case -1:
                speed = 1.1f;
                break;
        }
    }
}

しまづ君:教えてくれてありがとう! if文でも出来そうだなぁ

演習問題:whileがわかっているか

しまづくん:10から1000までの数字を表示したいんだけど、なんとか14までは表示出来たんだ!偉いでしょ^^
ネットで調べたらwhileが使えるって書いてあったんだけど、どうすればいいかな?

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int count = 10;

    void Start()
    {
        Debug.Log(count);
        count = count + 1;
        Debug.Log(count);
        count = count + 1;
        Debug.Log(count);
        count = count + 1;
        Debug.Log(count);
        count = count + 1;
        Debug.Log(count);
        count = count + 1;
    }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int count = 10;
    void Start()
    {
        while (count <= 1000)
        {
            Debug.Log(count);
            count = count + 1;
        }
    }
}

しまづくん:ありがとう!whileの使い方がわかったよ^^

演習問題:whileがわかっているか

しまづくん:1000から2000までの数字を表示し続けて、273の倍数があったらそこで処理止めたいってときはどうすればいいかな?

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int n = 1000;

    void Start()
    {
        while (n <= 2000)
        {
            Debug.Log(n);
            n++;
        }
    }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int n = 1000;

    void Start()
    {
        while (n <= 2000)
        {
            Debug.Log(n);
            if (n % 273 == 0)
            {
                break;
            }
            n++;
        }
    }
}

しまづくん:ありがとう!whileとbreakの使い方がわかったよ^^

演習問題:forがわかっているか

しまづくん:前の問題「10から1000までの数字を表示」はfor文でもできるって聞いたんだけどどうやったら出来るの?

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    int count = 10;

    void Start()
    {
        while (count <= 1000) {
            Debug.Log(count);
            count = count + 1;
        }
   }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    void Start()
    {
        for (int i=10; i<=1000; i++)
        {
            Debug.Log(i);
        }
   }
}

しまづくん:すごーい!for文の使い方がわかったよ^^

演習問題:forとbreakがわかっているか

しまづくん:前の問題「1000から2000までの数字を表示し続けて、273の倍数があったらそこで処理止めたい」もfor文ならどうなるの?

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    void Start()
    {
        for (int i=10; i<=1000; i++)
        {
            Debug.Log(i);
        }
   }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    void Start()
    {
        for (int i = 1000; i <= 2000; i++)
        {
            Debug.Log(i);
            if ( i%273 == 0)
            {
                break;
            }
        }
    }
}

しまづくん:すごーい!breakの使い方がわかったよ^^

演習問題:forとcontinueがわかっているか

しまづくん:0から99までの数字を表示したい。だけど、3の倍数は表示したくない場合、for文ならどうかけるの?
continueを使いたいなぁ

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    void Start()
    {
   }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    void Start()
    {
        for (int i = 0; i < 100; i++)
        {
            if ( i%3 == 0)
            {
                continue;
            }
            Debug.Log(i);
        }
    }
}

しまづくん:すごーい!continueの使い方がわかったよ^^

演習問題:列挙型の使い方

しまづくん:switch文で書いたコードがあるんだけど、0とか1とかわかりにくくて、、、列挙型ってものを使えば読みやすいコードになるって聞いたんだけどどうすればいいかな?途中までは書いてみたんだ!偉いでしょ!!!

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    enum Direction
    {
        UP,
        DOWN,
        RIGHT,
        LEFT
    }

    int direction = 0;
    void Start()
    {
        switch (direction)
        {
            case 0:
                Debug.Log("上");
                break;
            case 1:
                Debug.Log("下");
                break;
            case 2:
                Debug.Log("左");
                break;
            case 3:
                Debug.Log("右");
                break;
            default:
                Debug.Log("その他");
                break;
        }
    }
}

---解答ルパン---

Question.cs
using UnityEngine;

public class Question : MonoBehaviour
{
    enum Direction
    {
        UP,
        DOWN,
        RIGHT,
        LEFT,
    }

    Direction direction = Direction.DOWN; // ここはDirection.DOWNでなくてもOK
    void Start()
    {
        switch (direction)
        {
            case Direction.UP:
                Debug.Log("上");
                break;
            case Direction.DOWN:
                Debug.Log("下");
                break;
            case Direction.LEFT:
                Debug.Log("左");
                break;
            case Direction.RIGHT:
                Debug.Log("右");
                break;
            default:
                Debug.Log("その他");
                break;
        }
    }
}

しまづくん:すごーい!〇〇がわかったよ^^(わかった方は編集リクエストください)

つづき

28
12
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
12