# メソッド

## 練習問題

### C#を読みましょう

```c#
int Sum(int a, int b)
{
    return a + b;
}
```

説明：

<table border="1" id="bkmrk-%EF%BC%92%E3%81%A4%E3%81%AE%E6%95%B4%E6%95%B0%EF%BC%88a%E3%81%A8b%EF%BC%89%E3%82%92%E8%B6%B3%E3%81%97%E3%81%A6%E3%80%81%E3%81%9D%E3%81%AE%E7%B5%90%E6%9E%9C%E3%82%92" style="border-collapse: collapse; width: 100%;"><tbody><tr><td>２つの整数（aとb）を足して、その結果を整数として返す</td></tr></tbody></table>

```c#
int Max(int a, int b)
{
    if (a > b)
        return a;
    else
        return b;
}
```

<details id="bkmrk-%E7%AD%94%E3%81%88-%EF%BC%92%E3%81%A4%E3%81%AE%E6%95%B4%E6%95%B0%EF%BC%88a%E3%81%A8b%EF%BC%89%E3%81%AE%E6%9C%80%E3%82%82%E5%A4%A7%E3%81%8D%E3%81%84%E6%95%B0"><summary>答え</summary>

２つの整数（aとb）の最も大きい数値（最大値）を返す

</details>```c#
bool CheckHasItem(string[] itemList, string item)
{
    for (int i = 0; i < itemList.Length; i++)
    {
        if (itemList[i] == item)
            return true;
    }
    return false;
}
```

<details id="bkmrk-%E7%AD%94%E3%81%88-%E3%80%8Citemlist%E3%80%8D%E9%85%8D%E5%88%97%E3%81%AE%E4%B8%AD%E3%81%AB%E3%80%81%E3%80%8C"><summary>答え</summary>

「itemList」配列の中に、「item」という文字列が入っているかどうかを確認し、あったら「true」を返し、なければ、「false」を返す。つまり、アイテム存在しているかどうかを確認。

</details>### Unityで練習

１．配列で作った「[最も長いフルーツ](https://class.illogic.games/books/c/page/0353a#bkmrk-c%23%E3%81%AE%E5%87%A6%E7%90%86%E3%82%92%E5%AE%8C%E6%88%90%E3%81%97%E3%81%A6%E3%81%BF%E3%81%BE%E3%81%97%E3%82%87%E3%81%86)」のスクリプトを更新し、「`FindLongString`」というメソッドにしよう。配列を引数として渡し、最も長い文字列を返すようにてください。

使い方の例：

```c#
void Start()
{
    string [] fruits = {"lemon", "mikan", "orange", "grapefruit", "kiwi"};

    // このメソッドを作ってください
    string maxFruit = FindLongString(fruits);　  

    // grapefruitを表示されるはず
    Debug.Log($"最も長いフルーツ名は: {maxFruit}");
}

// ここからメソッドを作る...
```

<details id="bkmrk-%E7%AD%94%E3%81%88-%2F%2F-%E6%9C%80%E3%82%82%E9%95%B7%E3%81%84%E6%96%87%E5%AD%97%E5%88%97%E3%82%92%E6%8E%A2%E7%B4%A2-str"><summary>答え</summary>

```c#
// 最も長い文字列を探索
string FindLongString (string [] fruits)
{
  　// 最も長い文字列
    string maxFruit = "";
    
    // フルーツ1個ずつ確認
    foreach(string f in fruits)
    {
        // もし、今回のフルーツ名の方が長ければ
        if (f.Length > maxFruit.Length)
        {
            // 最も長いフルーツを更新
            maxFruit = f;
        }
    }

  　// 結果を返す
    return maxFruit;
}
```

</details>---

２．「じゃんけん」ゲーム結果の判定メソッド「`CheckJanken`」を作成してみてください。

 前提条件：

- グーは「0」、チョキは「1」、パーは「2」として表現する
- グー、チョキ、パー（0, 1, 2）以外の数値入れたら、エラーを返す
- プレーヤーは２人（AとB）
- 判定の結果は：Aの勝ち：1、Bの勝ち：２、ひきわけ：0、エラー：-1
- メソッド名：`CheckJanken`
    - 引数：各プレーヤーの選択肢
    - 戻り値：判定の結果

使い方の例１（Aの勝ち）

```c#
void Start()
{
  int playerA = 1; // チョキ
  int playerB = 2; // パー
  int result = CheckJanken(playerA, playerB);　// 1を返す（Aの勝ち）
  Debug.Log (result)
}
```

使い方の例２（Bの勝ち）

```c#
void Start()
{
  int playerA = 0; // グー
  int playerB = 2; // パー
  int result = CheckJanken(playerA, playerB);　// 2を返す（Bの勝ち）
  Debug.Log(result);
}
```

使い方の例３（あいこ）

```c#
void Start()
{
  int playerA = 2; // パー
  int playerB = 2; // パー
  int result = CheckJanken(playerA, playerB);　// 0を返す（あいこ）
  Debug.Log(result);
}
```

使い方の例４（エラー）

```c#
void Start()
{
  int playerA = 4; // ???
  int playerB = 2; // パー
  int result = CheckJanken(playerA, playerB);　// -1を返す（エラー）
  Debug.Log(result);
}
```

<details id="bkmrk-%E7%AD%94%E3%81%88-%2F%2F-%E3%81%98%E3%82%83%E3%82%93%E3%81%91%E3%82%93%E3%81%AE%E7%B5%90%E6%9E%9C%E3%81%AE%E5%88%A4%E5%AE%9A%E3%82%92%E6%B1%82%E3%82%81"><summary>答え</summary>

```c#
// じゃんけんの結果の判定を求める
// playerA: プレーヤーAの選択肢（0:グ, 1:チョキ, 2:パー)
// playerB: プレーヤーBの選択肢（0:グ, 1:チョキ, 2:パー)
// 戻り値：
//  -1：エラー（無効な選択肢)
//   0：あいこ
//   1：プレーヤーAの勝ち
//   2：プレーヤーAの勝ち
int CheckJanken (int playerA, int playerB)
{
    // エラーを確認（プレーヤーA）
    if (playerA < 0 || playerA > 2)
    {
        return -1;
    }
  　// エラーを確認（プレーヤーB）
    if (playerB < 0 || playerB > 2)
    {
        return -1;
    }
    
    // あいこ
    if (playerA == playerB)
    {
        return 0;
    }
    
    // プレーヤーAの勝ち
    if ((playerA == 0 && playerB == 1) || (playerA == 1 && playerB == 2) || (playerA == 2 && playerB == 0))
    {
        return 1;
    }
    
    // プレーヤーBの勝ちしかのこらない！
    return 2;
}
```

</details>