使用字母分数进行代码加密
Code encryption using letter scores
我有一个逻辑问题。最简单的方法是把一个字符串变成它的"score" in java,用于加密和解密。这就是我所说的分数。
A = 1;
B = 2;
C = 3;
等等
我想将整个字符串排序为一个分数,所以 "AABC" = 1 + 1 + 2 + 3 = 7
我意识到我可以将 A 设置为 1,将 Z 设置为 26,但这会很乏味并且浪费代码。
你可以这样做:
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);
}
试试这个,
char[] charArray = s.toCharArray();
int total = 0;
for(char c : charArray)
{
total = total + ((int)c) - 64;
}
System.out.println("Total : "+total);
我有一个逻辑问题。最简单的方法是把一个字符串变成它的"score" in java,用于加密和解密。这就是我所说的分数。
A = 1;
B = 2;
C = 3;
等等
我想将整个字符串排序为一个分数,所以 "AABC" = 1 + 1 + 2 + 3 = 7
我意识到我可以将 A 设置为 1,将 Z 设置为 26,但这会很乏味并且浪费代码。
你可以这样做:
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);
}
试试这个,
char[] charArray = s.toCharArray();
int total = 0;
for(char c : charArray)
{
total = total + ((int)c) - 64;
}
System.out.println("Total : "+total);