简体   繁体   English

如何使用java按升序生成数字的顺序?

[英]How to generate a sequential order of numbers in ascending order using java?

I want to create a java method to generate sequential numbers in ascending order.我想创建一个java方法来按升序生成序列号。 Which I can use to generate automatic TestStep IDs for my TestNG automation.我可以使用它为我的 TestNG 自动化生成自动 TestStep ID。 I just want to call this method and the numberings should happen sequentially for each teststep我只想调用此方法,并且每个测试步骤的编号应该按顺序发生

The question is a bit unclear.问题有点不清楚。 I'm assuming you want to count the number of tests and this number is going to be their ID.我假设你想计算测试的数量,这个数字将成为他们的 ID。 You could have some static and non-static variables and use static one as a counter.您可以有一些静态和非静态变量,并使用静态变量作为计数器。 Something like this:像这样的东西:

class Test {
    private static int ID_POOL = 0;
    public final int uniqueID;
    
    public Test() {
       uniqueID = ID_POOL++; // generate ID
    }
}

Alternatively:或者:

  private static int ID_POOL = 0;
    
  int generateID() {
    return ID_POOL++;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM