IF 语句

Tutorial

Beginner

+0XP

5 mins

312

Unity Technologies

IF 语句

如何使用 IF 语句在代码中设置条件。

本教程包含在“初级编程”项目中。

上一教程:约定和语法

下一教程:循环

Languages available:

1. IF 语句

using UnityEngine;
using System.Collections;

public class IfStatements : MonoBehaviour
{
    float coffeeTemperature = 85.0f;
    float hotLimitTemperature = 70.0f;
    float coldLimitTemperature = 40.0f;
    

    void Update ()
    {
        if(Input.GetKeyDown(KeyCode.Space))
            TemperatureTest();
        
        coffeeTemperature -= Time.deltaTime * 5f;
    }
    
    
    void TemperatureTest ()
    {
        // 如果咖啡的温度高于最热的饮用温度...
        if(coffeeTemperature > hotLimitTemperature)
        {
            // ... 执行此操作。
            print("Coffee is too hot.");
        }
        // 如果不是,但咖啡的温度低于最冷的饮用温度...
        else if(coffeeTemperature < coldLimitTemperature)
        {
            // ... 执行此操作。
            print("Coffee is too cold.");
        }
        // 如果两者都不是,则...
        else
        {
            // ... 执行此操作。
            print("Coffee is just right.");
        }
    }
}

Complete this Tutorial