这是提示
编写一个读取名称并根据名称输出ID的程序。 ID应为大写,并使用名称的前三个字母和从005开始的三个数字进行格式化。如果ID已存在,则数字应增加5。
这是我目前的代码
import java.util.Scanner;
public class substring_match {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int id = 5;
int repeatId = 5;
int nameCount = 1;
String[] nameArray = new String[5];
String name;
String subName = null;
for (int i = 0; i<nameArray.length; i++){
name = scan.nextLine();
subName = name.substring(0, 3);
nameArray[i] = subName;
}
for(int x = 0; x < nameArray.length; x++){
if(nameArray[x].substring(0, 3) == subName.substring(0,3)){
nameCount = nameCount + 1;
System.out.println("nameCount " + nameCount);
repeatId = nameCount * id;
if(repeatId == 5){
System.out.println(nameArray[x].toUpperCase() + "00" + repeatId);
}else{ // if repeatId is 10 or higher, won't need the extra 0
System.out.println(nameArray[x].toUpperCase() + "0" + repeatId);
}
}else{
System.out.println(nameArray[x].substring(0, 3).toUpperCase() + "00" + id);
}
}
}
}
答案 0 :(得分:0)
我可能会考虑采取另一种方式......
Have an empty hashmap<String sub, int multiplier>
Read in the name
Generate the subname
Fetch from or create subname in hashmap
If new, set multiplier to 1
If exists, increment multiplier
Return String.format(“%s%3d”, subname, multiplier x 5).toUpperCase()
我会给你代码,但在iPad上写上面的内容很难