我有个合乎逻辑的问题。在java中将字符串转换为其“分数”的最简单方法是什么,以便使用加密和解密。这就是我所说的分数。
A = 1;
B = 2;
C = 3;Ect。
我想将整个字符串排序为一个分数,所以"AABC“将=1+1+2+3=7
我意识到我可以将A设置为1,Z设置为26,但这将是乏味的,也是对代码的浪费。
发布于 2015-01-30 05:58:39
你可以这样做:
public static void main (String[] args)
{
String s = "AABC";
long score = 0;
for(int i = 0; i < s.length(); ++i)
{
score += s.charAt(i) - 'A' + 1;
//Basically, you check every index of the string and convert
//each character into its score and add them.
}
System.out.println(score);
}发布于 2015-01-30 06:00:12
尝尝这个,
char[] charArray = s.toCharArray();
int total = 0;
for(char c : charArray)
{
total = total + ((int)c) - 64;
}
System.out.println("Total : "+total);https://stackoverflow.com/questions/28229976
复制相似问题