为什么要递归? using System; using System.Collections.Generic;namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // First generate frist ear TheCell Chaos.Genesis(); int timeGoes = 1; while (timeGoes <= 20) { Console.WriteLine("The " + timeGoes.ToString() + " Day"); Chaos.LiveOneDay(); Console.WriteLine("\tThe total TheCell Count is: " + Chaos.CountTheCell()); timeGoes++; } Console.WriteLine("The 20th day, total cell count is " + Chaos.CountTheCell() + Environment.NewLine + "Press enter key quit."); Console.ReadLine(); } } public static class Chaos { private static int DayTick = 0; private static List<TheCell> AllTheCells = new List<TheCell>(); private static List<TheCell> NewBreadedCells = new List<TheCell>(); public static void Genesis() { AllTheCells.Add(new TheCell()); } public static void LiveOneDay() { foreach (TheCell cell in AllTheCells) { cell.Live(); if (cell.CanBread) NewBreadedCells.Add(new TheCell()); } AllTheCells.AddRange(NewBreadedCells); NewBreadedCells.Clear();
DayTick++; } public static int CountTheCell() { return AllTheCells.Count; } } public class TheCell { int _age = 0; public void Live() { _age++; } public bool CanBread { get { return _age > 4; } } } }
using System;
using System.Collections.Generic;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// First generate frist ear TheCell
Chaos.Genesis();
int timeGoes = 1;
while (timeGoes <= 20)
{
Console.WriteLine("The " + timeGoes.ToString() + " Day");
Chaos.LiveOneDay();
Console.WriteLine("\tThe total TheCell Count is: " + Chaos.CountTheCell());
timeGoes++;
} Console.WriteLine("The 20th day, total cell count is " + Chaos.CountTheCell() + Environment.NewLine + "Press enter key quit.");
Console.ReadLine();
}
} public static class Chaos
{
private static int DayTick = 0;
private static List<TheCell> AllTheCells = new List<TheCell>();
private static List<TheCell> NewBreadedCells = new List<TheCell>(); public static void Genesis()
{
AllTheCells.Add(new TheCell());
} public static void LiveOneDay()
{
foreach (TheCell cell in AllTheCells)
{
cell.Live();
if (cell.CanBread)
NewBreadedCells.Add(new TheCell());
}
AllTheCells.AddRange(NewBreadedCells);
NewBreadedCells.Clear();
DayTick++;
} public static int CountTheCell()
{
return AllTheCells.Count;
}
} public class TheCell
{
int _age = 0; public void Live()
{
_age++;
} public bool CanBread
{
get { return _age > 4; }
}
}
}