示例#1
0
        static void Main(string[] args)
        {
            try
            {
                var start = 1;
                var input = args[0];
                int.TryParse(input, out int end);
                var outMessage = string.Empty;
                var enumerator = new FizzBuzzEnumerator(start, end);

                while (enumerator.MoveNext())
                {
                    outMessage = enumerator.Current;
                    Console.WriteLine(outMessage);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error message: {ex.Message}", "StackTrace: {ex.StackTrace}");
            }
            finally
            {
                Console.WriteLine("-------------");
                Console.WriteLine("Please press any key to exit.");
                Console.ReadKey();
            }
        }
示例#2
0
        /// <summary>
        /// The start of the program
        /// </summary>
        public static void Main()
        {
            // Helper lambda in order to make tuples
            Func <int, string, Tuple <int, string> > makeTuple = (a, b) => new Tuple <int, string>(a, b);

            // Our standard FizzBuzz mapping:
            //   if a number is divisible by 3, print "Fizz"
            //   if a number is divisible by 5, print "Buzz"
            var mapping = new List <Tuple <int, string> > {
                makeTuple(3, "Fizz"),
                makeTuple(5, "Buzz")
                //, makeTuple(7, "Baz")
            };

            // In C# 6 the following should work, but in VS2013 it does not
            // This uses the extension method Add<T1,T2> below
            //var mapping = new List<Tuple<int, string>> {
            //    {3, "Fizz"},
            //    {5, "Buzz"}
            //    //,{7, "Baz"}
            //};



            // Construct the enumerator
            var fizzBuzz = new FizzBuzzEnumerator(100, mapping);

            // Print the output to the console
            foreach (string fizzBuzzOutput in fizzBuzz)
            {
                Console.WriteLine(fizzBuzzOutput);
            }

            Console.Read();
        }