首先,对我的英语感到抱歉。
我试图获取字符串的每个字母,并为每个字母给出一些 int 值。
例如:姓名 = Joao。 J=3,o=1,a=4,o=1。
我稍后会使用这些数字进行一些计算。
这就是我到目前为止得到的:
static String separador(String nome){
name = nome;
char[] letras;
letras = name.toCharArray();
int qtd = letras.length;
String fullName = "";
for(int i=0; i<qtd; i++)
{
fullName = fullName + " " + letras[i];
}
return fullName;
}
这让我分别得到每个字符。但可以得到其余的。
有人可以帮助我吗?
最佳答案
创建一个存储所有字符(A 到 Z)和相应数字的 map ,例如:
Map m = new HashMap(); m.put('A', 0); m.put('B', 1);
迭代输入的字符并检查该字符是否存在于上面的映射中。如果存在,则创建第二个 map ,并将该字符添加到第二个 map
static String separador(String nome){ name = nome; char[] letras; Map tempMap = new HashMap(); Map m = new HashMap(); m.put('A', 0); m.put('B', 1); letras = name.toCharArray(); int qtd = letras.length; String fullName = ""; for(int i=0; i<qtd; i++) { if(m.containsKey(letras[i]) tempMap.put(letras[i], m.get(letras[i])) } return tempMap;
}
关于JAVA给字符串的每个字符一个数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32486485/