Java 使用for循环生成不同的对象名称
我需要生成ID或对象名Java 使用for循环生成不同的对象名称,java,Java,我需要生成ID或对象名 public String[] getID(){ String[] tempArray; for(int x=0; x<staffNum;x++){ String temp = ("Att" + [x]); tempArray += temp; } return tempArray; } 因此for循环应该运行,并使用att添加迭代编号。 那么这应该是一个数组。 但问题出在+上 它
public String[] getID(){
String[] tempArray;
for(int x=0; x<staffNum;x++){
String temp = ("Att" + [x]);
tempArray += temp;
}
return tempArray;
}
因此for循环应该运行,并使用att添加迭代编号。
那么这应该是一个数组。
但问题出在+上
它在令牌上表示语法错误+
如何生成我的ID,请?用
String temp = ("Att" + Integer.toString(x));
语法已关闭。
您还必须初始化数组。
方法的命名不理想
//you're getting more than one id so indicate in method name
public String[] getIds(){
//initialize array
String[] tempArray = new String[staffNum];
//use i for indexes
for(int i=0; i<staffNum; i++){
//append the int onto the String
String tempString = ("Att" + i);
//set the array element
tempArray[i] = tempString;
}
return tempArray;
}
我相信你想做的是:
public String[] getID(){
// Create an array of String of length staffNum
String[] tempArray = new String[staffNum];
for(int x = 0; x < staffNum; x++){
// Affect the value Att[x] to each element of my array
tempArray[x] = String.format("Att[%d]", x);
}
return tempArray;
}
初始化size staffNum的数组
在循环中:
使用温度=附件+x
使用tempArray[x]=temp;
字符串温度=附件+x;tempArray[x]=温度;还需要初始化数组,以便String[]tempArray=newstring[staffNum];这个代码在很多方面都是错误的。考虑阅读一些java基础知识。他的代码有很多问题,虽然这是其中之一,但并不能解决所有问题。我刚刚解决了他现在正在编译的编译错误。不是别人