Switch 文

確認済のバージョン: 4.2

-

難易度: 初級

Switch 文は条件分岐と似た働きをします。Switch 文は 1 つの変数を複数の定数と比較する場合に便利です。ここでは、Switch 文の書き方と使い方を学びます。

Switch 文

初級 Scripting

ConversationScript

Code snippet

using UnityEngine;
using System.Collections;

public class ConversationScript : MonoBehaviour 
{
    public int intelligence = 5;
    
    
    void Greet()
    {
        switch (intelligence)
        {
        case 5:
            print ("Why hello there good sir! Let me teach you about Trigonometry!");
            break;
        case 4:
            print ("Hello and good day!");
            break;
        case 3:
            print ("Whadya want?");
            break;
        case 2:
            print ("Grog SMASH!");
            break;
        case 1:
            print ("Ulg, glib, Pblblblblb");
            break;
        default:
            print ("Incorrect intelligence level.");
            break;
        }
    }
}
#pragma strict

var intelligence : int = 5;
    
    
function Greet()
{
    switch (intelligence)
    {
    case 5:
        print ("Why hello there good sir! Let me teach you about Trigonometry!");
        break;
    case 4:
        print ("Hello and good day!");
        break;
    case 3:
        print ("Whadya want?");
        break;
    case 2:
        print ("Grog SMASH!");
        break;
    case 1:
        print ("Ulg, glib, Pblblblblb");
        break;
    default:
        print ("Incorrect intelligence level.");
        break;
    }
}
import UnityEngine
import System.Collections


public class ConversationScript(MonoBehaviour):

    public intelligence = 5

    
    
    private def Greet():
        converterGeneratedName1 = intelligence
        if converterGeneratedName1 == 5:
            print('Why hello there good sir! Let me teach you about Trigonometry!')
        elif converterGeneratedName1 == 4:
            print('Hello and good day!')
        elif converterGeneratedName1 == 3:
            print('Whadya want?')
        elif converterGeneratedName1 == 2:
            print('Grog SMASH!')
        elif converterGeneratedName1 == 1:
            print('Ulg, glib, Pblblblblb')
        else:
            print('Incorrect intelligence level.')

関連するチュートリアル