我这里有一个 C# 控制台应用程序,当我允许用户输入一定数量的记录时,程序会生成一个报价单号。这是为了让我在我的程序中实现它之前测试我制定的算法。
例如,当我输入 23(假设这是数据库中的当前记录数)时,程序将输出“CQ-13-04-0023”。但我得到了“CQ-13-04-0051”的输出。我的目标是当给定报价编号中的序列号达到 9999 时,当数据库中的记录数达到 10000 及以上时,它将重置为 0001。
这是我的代码:
class Program
{
static void Main(string[] args)
{
string QuoteRefNum = "";
string seriesCount = "";
DateTime year = DateTime.Now;
string strYear = year.ToString("yy");
string strMonth = year.ToString("MM");
Console.Write("Input the number of records: ");
int numberOfRecords = Convert.ToInt32(Console.Read());
numberOfRecords++;
if (numberOfRecords == 0)
{
seriesCount = "000" + numberOfRecords++;
}
else if (numberOfRecords >= 1 && numberOfRecords <= 9)
{
seriesCount = "000" + numberOfRecords;
}
else if (numberOfRecords >= 10 && numberOfRecords <= 99)
{
seriesCount = "00" + numberOfRecords;
}
else if (numberOfRecords >= 100 && numberOfRecords <= 999)
{
seriesCount = "0" + numberOfRecords;
}
else if (numberOfRecords >= 1000 && numberOfRecords <= 9999)
{
seriesCount = numberOfRecords.ToString();
}
else if (numberOfRecords >= 10000 && numberOfRecords <= 99999)
{
string newSetOfRecords = numberOfRecords.ToString();
int tempNumber = 0;
newSetOfRecords.Remove(0, 1);
tempNumber = Convert.ToInt32(newSetOfRecords);
if (tempNumber == 0)
{
seriesCount = "000" + tempNumber++;
}
else if (tempNumber >= 1 && tempNumber <= 9)
{
seriesCount = "000" + tempNumber;
}
else if (tempNumber >= 10 && tempNumber <= 99)
{
seriesCount = "00" + tempNumber;
}
else if (tempNumber >= 100 && tempNumber <= 999)
{
seriesCount = "0" + tempNumber;
}
else if (tempNumber >= 1000 && tempNumber <= 9999)
{
seriesCount = tempNumber.ToString();
}
}
QuoteRefNum = "CQ" + strYear + "-" + strMonth + "-" + seriesCount;
Console.WriteLine("The quote reference number is: " + QuoteRefNum);
}
}