・p.120 chap3_8_1(配列とforeachによる2重ループ)から
提出フォロー:ミニ演習:フィボナッチ数列 mini117 続き
・数列の要素数を10にして、要素[2]以降の値のセットをfor文による繰返しで行うようにしよう
・ヒント: for(int i = 2; i < fib.Length; i++) { fib[i] = fib[i - 2] + fib[i - 1]; }
・実行結果: 1,1,2,3,5,8,13,21,34,55
・合計は、そのままforeachで得ると良い
作成例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mini117 : MonoBehaviour {
void Start() {
int[] fib = new int[10]; //要素数10のフィボナッチ数列を生成
fib[0] = fib[1] = 1; //配列fibの要素[0]と要素[1]に1を代入
for (int i = 2; i < fib.Length; i++) { //要素[2]以降の全要素について繰返す
fib[i] = fib[i - 2] + fib[i - 1]; //前2要素の和を代入
}
Debug.Log(string.Join(",", fib)); //配列fibの全要素を連結表示(カンマ区切り)
int sum = 0; //合計用
foreach(var work in fib) { //数列の全要素について作業変数を用いて繰返す
sum += work; //合計に足し込む
}
Debug.Log("計=" + sum); //合計を表示
}
void Update() {
}
}
p.120 chap3_8_1(配列とforeachによる2重ループ)から
・foreachによる多重ループが記述できる ・この場合、作業変数は別の名前にすること ・p.120 chap3_8_1では同じ配列を用いているが、同じでなくても良い
p.120 chap3_8_1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class chap3_8_1 : MonoBehaviour {
void Start() {
string[] team = { "A", "B", "C", "D" };
foreach (string t1 in team) { //全要素の分、作業変数t1を用いて繰返す(x4)
foreach (string t2 in team) { //全要素の分、作業変数t2を用いて繰返す(x4)
Debug.Log(t1 + "vs" + t2); //2重ループの中なので作業変数t1とt2が使える(x16)
}
}
}
void Update() {
}
}
p.122 chap3_8_2:chap3_8_1をそのまま修正し同一チームの対戦を取り除く
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class chap3_8_1 : MonoBehaviour {
void Start() {
string[] team = { "A", "B", "C", "D" };
foreach (string t1 in team) { //全要素の分、作業変数t1を用いて繰返す(x4)
foreach (string t2 in team) { //全要素の分、作業変数t2を用いて繰返す(x4)
if (t1 != t2) { //同一チームでなければ
Debug.Log(t1 + "vs" + t2); //2重ループの中なので作業変数t1とt2が使える(x12)
}
}
}
}
void Update() {
}
}
p.123 chap3_8_3:chap3_8_1をそのまま修正し同一組み合わせも取り除く
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class chap3_8_1 : MonoBehaviour {
void Start() {
string[] team = { "A", "B", "C", "D" };
int start = 1; //開始添字
foreach (string t1 in team) { //全要素の分、作業変数t1を用いて繰返す(x4)
for (int cnt = start; cnt < 4; cnt++) { //開始添字以降の要素で、変数cntを用いて繰返す
Debug.Log(t1 + "vs" + team[cnt]); //2重ループの中なので作業変数t1とt2が使える(x12)
}
start++; //開始添字を先に進める
}
}
void Update() {
}
}
ミニ演習:chap3_8_1~3 改造 mini123
・forの2重ループにして変数startを用いないように改良しよう
作成例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mini123 : MonoBehaviour {
void Start() {
string[] team = { "A", "B", "C", "D" };
for (int start = 0; start < 4; start++) { //全要素の分、作業変数startを用いて繰返す(x4)
for (int cnt = start + 1; cnt < 4; cnt++) { //次の要素以降で、変数cntを用いて繰返す
Debug.Log(team[start] + "vs" + team[cnt]); //2重ループの中(x6)
}
}
}
void Update() {
}
}
p.124 無限ループを止める
・コンポーネントの削除によって対処する場合は、Inspectorのスクリプト名の右端の「…」をクリックして「Remove Conponent」 ※ Unityのバージョンアップで歯車アイコンから「…」の縦字に変更
p.125 配列の上限を超える場合
・配列の添字が要素数以上または負の数になった場合、実行時エラー「IndexOutOfRangeException(添字範囲外例外)」が発生する ・実行時エラーは文法エラーではないので、Visual Studioのエディタでは発生がわからない ※ 実行時エラーが起こることが明らかな場合はエラーや警告が表示されることもある ・実行時エラーの発生場所はUnity側に表示されるメッセージで確認できる ・書式: [発生時刻]発生したエラーを示す例外クラス名:メッセージ 実行クラス名.メソッド名(…)(at Assets/ソースファイル名:行番号) ・例: [12:50:02]IndexOutOfRangeException: Index was outside the bounds of the array. chap3_6_1.Start () (at Assets/chap3_6_1.cs:9)
p.126 復習ドリル 問題1 chap3_10_1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class chap3_10_1 : MonoBehaviour {
void Start() {
string[] grade = { "松", "竹", "梅" };
foreach (var g in grade) { //配列gradeの全要素について作業変数gを用いて繰返す
Debug.Log(g);
}
}
void Update() {
}
}
p.126 復習ドリル 問題1 chap3_10_2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class chap3_10_2 : MonoBehaviour {
void Start() {
string[] dirs = { "東", "西", "南", "北"};
for (int cnt = dirs.Length - 1; cnt >= 0 ; cnt--) { //変数cntを用いて逆順で繰返す
Debug.Log(dirs[cnt] + "方向");
}
}
void Update() {
}
}