码迷,mamicode.com
首页 > 编程语言 > 详细

【Unity3D/C#】利用IEnumerable<>和yield产生斐波那契数列

时间:2016-09-03 18:25:35      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class YieldTest : MonoBehaviour {
    public bool b = true;
    public IEnumerator myIEnumertor = null;

    void Awake() 
    {
        
    }

    // Use this for initialization
    void Start () 
    {
        myIEnumertor = GenerateFibonacci().GetEnumerator();

        int i = 0;
        do
        {
            myIEnumertor.MoveNext();
            Debug.Log(myIEnumertor.Current);
            i++;
        }
        while (i < 10);
    }

    // Update is called once per frame
    void Update () 
    {
    
    }

    public static IEnumerable<int> GenerateFibonacci()
    {
        yield return 0;
        yield return 1;
        int last0 = 0, last1 = 1, current;
        while (true)
        {
            current = last0 + last1;
            yield return current;
            last0 = last1;
            last1 = current;
        }
    }
}

 

【Unity3D/C#】利用IEnumerable<>和yield产生斐波那契数列

标签:

原文地址:http://www.cnblogs.com/qiuxiangmuyu/p/5837627.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!