我需要生成的实例名称在每个时间都不同,例如blocktitle1,blocktitle2,blocktitle3等等。我已经添加了一些代码来更改字符串变量“title”,但只是将单词“Title”放在其中,我创建一个新的位图将使名为“Title”的实例不是例如“blocktitle2”。
我会非常感激,任何人都可以提供帮助。
List<Block> blocks = new LinkedList<Block>();
Random rnd = new Random(System.currentTimeMillis());
int randomx = 400;
public Block block;
int blocknum = 10;
String Title = "blocktitle" + blocknum;
public void generateBlocks(){
if(blocknum > 0){
int offset = rnd.nextInt(400) + 100; //500 is the maximum offset, this is a constant
x += offset; //ofset will be between 100 and 400
// i need the word below "block" to be recognised as a String variable "String Title"
block = new Block(BitmapFactory.decodeResource(getResources(), R.drawable.block), randomx, 200);
blocknum -= 1;
}
}
答案 0 :(得分:0)
试试这个:
private static final String titlePrefix = "blocknum";
private static int titleNo = 0;
public String getNextTitle() {
return titlePrefix + titleNo++;
}