我要求我需要生成一个订单号(顺序),而不是随机的,在下订单之前应该向用户显示。
private string GenerateOrderNumber()
{
string OrderNumber;
//JI-XXXXXXXXX-XXXX
Random rnd = new Random();
long orderPart1 = rnd.Next(100000, 9999999);
int orderPart2 = rnd.Next(1000, 9999);
OrderNumber = "JI-" + orderPart1 + "-" + orderPart2;
return OrderNumber;
}
答案 0 :(得分:1)
怎么样:
long orderPart1 = 100000;
int orderPart2 = 1000;
Debug.WriteLine(string.Format("JI-{0}-{1}", orderPart1, orderPart2));
orderPart2++;
Debug.WriteLine(string.Format("JI-{0}-{1}", orderPart1, orderPart2));
orderPart2 = 9999;
if(orderPart2 == 9999)
{
orderPart1++;
orderPart2 = 1000;
}
Debug.WriteLine(string.Format("JI-{0}-{1}", orderPart1, orderPart2));
哪个输出:
JI-100000-1000
JI-100000-1001
JI-100001-1000
所以你可以创建一个类:
void Main()
{
OrderNumberGenerator orderNumberGenerator = new OrderNumberGenerator();
Debug.WriteLine(orderNumberGenerator.Next());
Debug.WriteLine(orderNumberGenerator.Next());
}
// Define other methods and classes here
public class OrderNumberGenerator
{
long orderPart1 = 100000;
int orderPart2 = 1000;
public string Next()
{
if(orderPart2 == 9999)
{
orderPart1++;
orderPart2 = 1000;
}
else
{
orderPart2++;
}
return string.Format("JI-{0}-{1}", orderPart1, orderPart2);
}
}
哪个输出:
JI-100000-1000
JI-100000-1001
答案 1 :(得分:0)
以下是按顺序生成所有ID的方法:
for (long n = 0; n <= 9999999999; n++)
{
string digits = n.ToString("0000000000");
string output = $"JI-{digits.Substring(0, 6)}-{digits.Substring(6, 4)}";
Console.WriteLine(output);
}