Unity的C#編程教程_38_循環語句挑戰:列印單雙數
- 2020 年 9 月 4 日
- AI
- C/C++/C#
- 任務:計數列印從0~10,然後列印出 10~ 20 之間的雙數,20~30之間的單數
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PrintNum : MonoBehaviour
{
private int num = 0; // 初始化為 0
// Start is called before the first frame update
void Start()
{
while (num < 31) // 0~30 循環
{
if (num <= 10) // 10 以下都列印
{
Debug.Log("Num = " + num);
}
else if (num <= 20)
{
if (num % 2 == 0) // 判斷為雙數
{
Debug.Log("Even Num = " + num);
}
}
else
{
if (num % 2 == 1) // 判斷為單數
{
Debug.Log("Odd Num = " + num);
}
}
num++;
}
}
// Update is called once per frame
void Update()
{
}
}