public static string FizzBuzzCheckNumber(int number, FizzBuzzCheck FizzBuzzCheck) { var tempResultBuilder = new StringBuilder(); foreach (var x in FizzBuzzCheck.rules) { if (x.Divisor != 0) { tempResultBuilder.Append(number % x.Divisor == 0 ? x.Output : ""); } if (tempResultBuilder.ToString().Length == 0) { tempResultBuilder.Append(number.ToString().Contains(x.StringCheck) ? x.Output : ""); } } string tempResultString = tempResultBuilder.ToString(); tempResultBuilder.Clear(); string finalResultString = (String.IsNullOrWhiteSpace(tempResultString) ? number.ToString() : tempResultString); return(finalResultString); }
static void Main(string[] args) { int i; string resultString; Console.WriteLine("Begin fizzbuzz Program."); Console.ReadKey(); var FizzBuzzCheck = new FizzBuzzCheck(); FizzBuzzCheck.AddRule(3, "fizz"); FizzBuzzCheck.AddRule(5, "buzz"); FizzBuzzCheck.AddRule("3", "fizz"); FizzBuzzCheck.AddRule("5", "buzz"); for (i = 1; i <= 100; i++) { resultString = FizzBuzzCheckNumber(i, FizzBuzzCheck); Console.WriteLine(resultString); } Console.WriteLine("End Program!"); Console.ReadKey(); }