Unity的C#编程教程_33_for循环语句挑战1
- 2020 年 8 月 20 日
- AI
- C/C++/C#
- 设计倒数程序,从 100 倒数到 1
- 设计程序,从 1 数到 100,仅显示双数
- 设计程序,从 1 数到 100,仅显示单数
- 设计程序,从 1 数到 100,仅显示66这个数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ForOverview : MonoBehaviour
{
public string theWord = "apple";
// Start is called before the first frame update
void Start()
{
for(int i = 100; i > 0; i--)
{
Debug.Log(i); // 从 100 倒数到 1
}
for(int i = 0; i < 100; i++)
{
if (i % 2 == 0) // i/2 余数为 0 表示可以被 2 整除,即为双数
{
Debug.Log(i);
}
}
for (int i = 0; i < 100; i++)
{
if (i % 2 == 1) // i/2 余数为 1 表示为单数
{
Debug.Log(i);
}
}
for (int i = 0; i < 100; i++)
{
if (i == 66) // 用于在一堆东西中查找特定目标
{
Debug.Log("i="+i);
}
}
Debug.Log("Loop finished!");
}
// Update is called once per frame
void Update()
{
}
}