講義メモ

・「ジャンケン」その3の補足をしてから、p.098「繰り返し文」に進みます

提出:追加演習「ジャンケン」その3 janken3

・判定を表示しよう
・例「あなたの勝ち」「わたしの勝ち」「あいこ」
・ヒント:先に「あいこ」を判断すると楽。判定はORで連結した条件ですると良い
 ※ 計算式で判定することもできる:(3 + hand - cpuh) % 3 == 2 ならばhandの勝ち

作成例1

using UnityEngine;
public class janken3 : MonoBehaviour {
    [Header("手をどうぞ(0=グー,1=チョキ,2=パー)")]
    public int hand;
    void Start() {
        System.Random r = new System.Random(); //乱数クラスのオブジェクトを生成
        string s = "あなたの手=";
        Debug.Log(s + ((hand == 0) ? "グー" : (hand == 1) ? "チョキ" : "パー"));
        string c = "わたしの手=";
        int cpuh = r.Next(3); //乱数で0,1,2を得る
        Debug.Log(c + ((cpuh == 0) ? "グー" : (cpuh == 1) ? "チョキ" : "パー"));
        if(hand == cpuh) {
            Debug.Log("あいこ");
        } else if (hand == 0 && cpuh == 1 || 
                   hand == 1 && cpuh == 2 ||
                   hand == 2 && cpuh == 0) { //条件で判定
            Debug.Log("あなたの勝ち");
        } else {
            Debug.Log("わたしの勝ち");
        }
    }
    void Update() { }
}

作成例2

using UnityEngine;
public class janken3 : MonoBehaviour {
    [Header("手をどうぞ(0=グー,1=チョキ,2=パー)")]
    public int hand;
    void Start() {
        System.Random r = new System.Random(); //乱数クラスのオブジェクトを生成
        string s = "あなたの手=";
        Debug.Log(s + ((hand == 0) ? "グー" : (hand == 1) ? "チョキ" : "パー"));
        string c = "わたしの手=";
        int cpuh = r.Next(3); //乱数で0,1,2を得る
        Debug.Log(c + ((cpuh == 0) ? "グー" : (cpuh == 1) ? "チョキ" : "パー"));
        if(hand == cpuh) {
            Debug.Log("あいこ");
        } else if ((3 + hand - cpuh) % 3 == 2) { //式で判定
            Debug.Log("あなたの勝ち");
        } else {
            Debug.Log("わたしの勝ち");
        }
    }
    void Update() { }
}

Chapter 3 繰返し文
p.100 while文

・前判定の単純な繰返しに向く構文
・前判定:繰返しの1回目の前に条件をチェックすること。1度も繰り返さないことがある場合に便利
・書式: while(継続条件) { 繰返し内容;… }
・継続条件はbool型(p.56)の値または式で、主に、比較演算子の式を用いる
・例: while(a < 3) { a = a + 1; } //aが0だったら3回繰り返す
・この例のように式がtrueである間=条件式が成立する間だけ繰返すというパターンが多い
・例: while(a > 0) { a = a - 1; } //aが3だったら3回繰り返す

p.101 chap3_2_1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class chap3_2_1 : MonoBehaviour {
    void Start() {
        int shikin = 30000;
        while(shikin >= 0) { //shikinが0以上であれば繰返す
            Debug.Log(shikin);
            shikin = shikin - 5080; //shikinから5080を差し引く
        } //whileブロック(繰返し内容)の終わり
    }
    void Update() {

    }
}

p.103 計算もできる代入演算子

・正式には複合代入演算子といい、左辺の変数を用いた式を代入の右辺に書く場合に、誤読しやすいことから、
 誤読を防ぎ、かつ、冗長さを省く仕掛け
・+=演算子: 〇 = 〇 + ◆ を 〇 += ◆ と書ける。意味は「足し込む」
 例: a = a + 5; ⇒ a += 5;
・-=演算子: 〇 = 〇 - ◆ を 〇 -= ◆ と書ける。意味は「差し引く」
 例: a = a - 5; ⇒ a -= 5;
・他に「*=(N倍する、掛けた積にする)」「/=(割った商にする)」「%=(割った余りにする)」などがある

アレンジ演習:p.101 chap3_2_1①

・-=演算子で書き直そう

作成例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class chap3_2_1 : MonoBehaviour {
    void Start() {
        int shikin = 30000;
        while(shikin >= 0) { //shikinが0以上であれば繰返す
            Debug.Log(shikin);
            shikin -= 5080; //【変更】shikinから5080を差し引く
        } //whileブロック(繰返し内容)の終わり
    }
    void Update() {

    }
}

アレンジ演習:p.101 chap3_2_1②

・資金をパブリック変数で与えるようにしよう
・そして、資金が負の数の時には何も表示されないことも確認しよう

作成例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class chap3_2_1 : MonoBehaviour {
    public int shikin = 30000; //【移動】
    void Start() {
        while(shikin >= 0) { //shikinが0以上であれば繰返す
            Debug.Log(shikin);
            shikin -= 5080; //shikinから5080を差し引く
        } //whileブロック(繰返し内容)の終わり
    }
    void Update() {

    }
}

アレンジ演習:p.101 chap3_2_1③

・whileブロックを抜けた後で、資金の額はどうなっているか確認しよう

作成例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class chap3_2_1 : MonoBehaviour {
    public int shikin = 30000;
    void Start() {
        while(shikin >= 0) { //shikinが0以上であれば繰返す
            Debug.Log(shikin);
            shikin -= 5080; //shikinから5080を差し引く
        } //whileブロック(繰返し内容)の終わり
        Debug.Log("繰返し後の資金 = " + shikin); //【追加】
    }
    void Update() {

    }
}

アレンジ演習:p.101 chap3_2_1④

・if文を加えて、資金が赤字(負の数)にならないようにしよう
・ヒント: 資金が5080以上あれば差し引くようにすれば良い
・しかし、こうすると、繰返しが終わらなくなるので、繰返し条件を「資金 > 0」に変更しよう
・そして、資金が5080未満であれば資金を0にすれば良い(あるだけ払うイメージ)

作成例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class chap3_2_1 : MonoBehaviour {
    public int shikin = 30000;
    void Start() {
        while(shikin > 0) { //【変更】shikinがある間繰返す
            Debug.Log(shikin);
            if (shikin >= 5080) { //【追加】shikinが5080以上ある?
                shikin -= 5080; //shikinから5080を差し引く
            } else { //【追加】shikinが5080未満?
                shikin = 0; //【追加】shikinを使い尽くす
            }
        } //whileブロック(繰返し内容)の終わり
        Debug.Log("繰返し後の資金 = " + shikin);
    }
    void Update() {

    }
}

ミニ演習 mini103

・3から0までカウントダウンし、続けて3までカウントアップしよう
・whileを続けて2回行えば良い

作成例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class mini103 : MonoBehaviour {
    void Start() {
        int i = 3;
        while (i > 0) { //0超である間繰返す=0になるまで
            Debug.Log(i);
            i = i - 1; //1を差し引く
        } //この繰返しを抜けた時点ではiは0になっている
        while (i <= 3) { //3以下ある間繰返す=4になるまで
            Debug.Log(i);
            i = i + 1; //1を足し込む
        }
    }
    void Update() {

    }
}

コメントを残す

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