using UnityEngine;
using System.Collections;
public class IFStatements : MonoBehaviour
{
public float coffeeTemperature = 85.0f;
private float hotLimitTemperature = 70.0f;
private float coldLimitTemperature = 40.0f;
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
TemperatureTest();
coffeeTemperature -= 5.0f * Time.deltaTime;
}
void TemperatureTest()
{
if (coffeeTemperature > hotLimitTemperature)////(1)
{
Debug.Log("Coffee is too hot.");
}
else if (coffeeTemperature < coldLimitTemperature)////(2)
{
Debug.Log("Coffee is too cold.");
}
else////(3)
{
Debug.Log("Coffee is just right.");
}
}
}
這是一個判斷咖啡溫度的程式,咖啡溫度coffeeTemperature會在Update中隨著時間遞減,按下空白鍵會執行TemperatureTest函式,可以判斷咖啡的溫度是否適中。
(1)使用if條件式判斷咖啡溫度是否太高,語法為:
if(條件)
{
條件成立時執行的內容
}
(2)當if條件不成立時,可以使用else if判斷別的條件,語法與上述相同。
(3)當前面的if條件都不成立時,就會執行else裡的內容。
接著是執行的結果。
以上就是if條件判斷式的說明。
這次就到這裡。

沒有留言:
張貼留言