当前位置:编程学习 > 网站相关 >>

谁能帮我解释一下,这个代码是什么意思,它是如何构架的,谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace NumberPuzzle
{
    class Program
    {
        /// <summary>
        /// Num Puzzle
        /// ^^**
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string numPazzle = string.Empty;
            string numLength = string.Empty;
            int count = 0;
            int countMax = 0;

            Console.WriteLine("How long do you want?[0<n<11] \"Suggestion : 4\"");

            while (true)
            {
                numLength = Console.ReadLine();
                if (IsNum(numLength))
                {
                    countMax = Convert.ToInt32(numLength);
                    if (countMax > 10)
                    {
                        Console.WriteLine("Re-inpt due to n>10");
                        continue;
                    }
                    break;
                }
                else
                {
                    Console.WriteLine("Re-inpt, input is not a num:");
                    continue;
                }
            }

            while (count < countMax)
            {
                string strA = GetNum();
                if (numPazzle.IndexOf(strA) != -1)
                {
                    continue;
                }
                numPazzle += strA;
                count++;
            }

            while (true)
            {
                string input = string.Empty;
                string results = string.Empty;
                Console.WriteLine("Input what you guess:");
                input = Console.ReadLine();

                if (!IsNum(input))
                {
                    Console.WriteLine("Re-inpt, input is not a num:");
                    continue;
                }

                if (input.Length != countMax)
                {
                    Console.WriteLine("The length of input is error");
                    continue;
                }
                
                if (IsDup(input))
                {
                    Console.WriteLine("Input is a dup num");
                    continue;
                }
                results = CompareNum(input, numPazzle);
                if (results.Split('-')[0].Equals(numPazzle.Length.ToString()))
                    break;

                Console.WriteLine("Results: A-{0} B-{1}", results.Split('-')[0], results.Split('-')[1]);
            }
            Console.WriteLine("Win! The num is {0}", numPazzle);
            Console.ReadKey();
        }

        public static string GetNum()
        {
            Random sSeed = new Random();
            Random seed = new Random(sSeed.Next());
            return seed.Next(10).ToString();
        }

        public static string CompareNum(string actualStr, string expectedStr)
        {
            int a = 0;
            int b = 0;
            string results = string.Empty;
            for (int i = 0; i < actualStr.Length; i++)
            {
                if (expectedStr.IndexOf(actualStr[i]) != -1)
                {
                    b++;
                }
                if (expectedStr[i].Equals(actualStr[i]))
                {
                    a++;
                    b--;
                }
            }
            results = a.ToString() + "-" + b.ToString();
            return results;
        }

        public static bool IsDup(string input)
        {
            bool result = false;
            foreach (char aStr in input)
            {
                if (input.IndexOf(aStr) != input.LastIndexOf(aStr))
                {
                    result = true;
                    break;
                }
            }
            return result;
        }

        public static bool IsNum(string numInput)
        {
            bool result = false;
            Regex reg = new Regex(@"^-?\d+$");
            result = reg.IsMatch(numInput);
            return result;
        }
    }
}
补充:云计算 ,  云安全
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,