【高分】C#小程序编一个程序

2024-11-19 03:29:08
推荐回答(5个)
回答1:

控制台程序还是窗体程序?


// 下面是控制台程序

using System;


namespace ConsoleApplication1
{
    class Program
    {
        static bool IsDigits(char n)
        {
            char[] dig = new char[10] {'0','1','2','3','4','5','6','7','8','9'};
            
            for (int i = 0; i < dig.Length; i++)
            {
                if (Convert.ToChar(n) == dig[i]) return true;
            }
            return false;
        }
        static void Main(string[] args)
        {
            char[] ID = new char[18];
            int i;
            bool YES=true;

            string aa;
            
            while (true)
            {
                Console.Write("请输入身份证号码:");
                for (i = 0; i < 18; i++) // 保证输入的字符有18个(暂且不管它是否为数字)
                {
                    ID[i] = (char)Console.Read();
                }

                // 判断它是不是全部都为数字
                for (i = 0; i < 18; i++)
                {
                    if (Program.IsDigits(ID[i])) continue;
                    else
                    {
                        YES = false;
                        break;
                    }
                    
                }
                
                if (YES)
                {
                    Console.WriteLine("身份证号码符合要求");
                    break;
                }
                else
                { // 不符合要求
                    Console.WriteLine("不符合要求");
                    Console.ReadKey(true);
                    Console.Clear();
                    continue;
                } 
                
                
                
            }
           
        }
    }
}

回答2:

身份证号最后一位可以是字母的,如果你要的确实只是18位数字,如下

static Boolean CheckID(string id)
        {
            string card = id.Trim();
            if (card.Length != 18) return false;
            foreach (char c in card)
                if (!(c >= '0' && c <= '9')) return false;
            return true;
        }
        static void Main(string[] args)
        {
            Console.Write("请输入18位数字:");
            while (true)
            {
                string input = Console.ReadLine();
                if (CheckID(input)) { Console.WriteLine("输入有效"); break; }
                else Console.Write("无效数字,请重新输入:");
            }            
            Console.ReadKey();
        }

回答3:

判断字符串是否为数字:

 

            string str ="123s";            
            try
            {
                int i = Convert.ToInt32(str);
                return false;
            }
            catch
            {
                return true;
            }

长度:

            string str ="123";
            if (str.Length != 18)
            {
                //Length是长度 长度是3不等于18
            }
            else
            {
                //等于18
            }

回答4:

        string shenfenzheng = text1.Text;

if(shenfenzheng.Length != 18)
{//输入长度不等于18
return false;
}

if(CheckInputChar(shenfenzheng))
{//检查是否全数字
return true;
}
else
{
return false;
}

/// 
        /// 验证用户输入
        /// 

        /// 
        /// 
        internal static bool CheckInputChar(string str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                char a = str[i];
                if ((a >= '0' && a <= '9'))
                {

                }
                else
                {
                    return false;
                }
            }
            return true;
        }

回答5:

有个问题就是身份证不全数字的,有得结尾是x