【问题标题】:Generating an alphanumeric Sequence in C# on button click单击按钮时在 C# 中生成字母数字序列
【发布时间】:2019-02-28 10:05:17
【问题描述】:

我想用c#创建一个字母数字。格式应该是

AAA00001 AAA00002 . . . AAA99999 AAB00001 . AAB99999 . . AAZ00001 . . ABA00001 . . ZZZ99999

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 看起来不错。什么东西阻止你?另外,ZZZ99999 之后的下一个值是什么?
  • @Aars93 我没试过我是 c# 环境的新手
  • @pranaybrahmbhatt StackOverflow 不是代码编写服务。我们为尝试编写自己的代码但遇到问题的人提供支持。我建议你遵循一些关于循环和字符串的教程。

标签: c# asp.net logic


【解决方案1】:

尝试以下:

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

namespace ConsoleApplication103
{
    class Program
    {
        static void Main(string[] args)
        {
            AlphaNumeric alphaNumeric = new AlphaNumeric();
            for (int i = 0; i < 100010; i++)
            {
                string sequence = alphaNumeric.GetSequence();
                Console.WriteLine(sequence);
            }
            Console.ReadLine();

        }
    }

    public class AlphaNumeric
    {
        const int NUMBER_PLACES = 5;
        int maxInteger = 1;
        Boolean OVER_FLOW = false;
        static string format = new string('0', NUMBER_PLACES);
        private static int number { get; set; }
        private static List<char> alpha { get; set; }

        public AlphaNumeric()
        {
            for(int i = 0; i < NUMBER_PLACES ; i++) {maxInteger *= 10;}
            number = 1;
            alpha = new List<char>() { 'A', 'A', 'A' };
        }
        public string GetSequence()
        {
            if(OVER_FLOW) return "OVERFLOW ERROR";
            string sequence = string.Join("", alpha) + number.ToString(format);
            Increment();
            return sequence;
        }
        public void Increment()
        {
            number++;
            if (number >= maxInteger)
            {
                number = 1;

                for (int i = alpha.Count - 1; i >= 0; i--)
                {
                    int ascii = (int)alpha[i] + 1;
                    if ((ascii <= (int)'Z'))
                    {
                        alpha[i] = (char)ascii;
                        break;
                    }
                    else
                    {
                        if (i == 0)
                        {
                            OVER_FLOW = true; ;
                        }
                        else
                        {
                            alpha[i] = 'A';
                        }
                    }                    
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多