我正在制作一种将字符翻译成相应数字的方法,例如a = 1,b = 2 ......
我一直在接受关于我的字典数组声明的IDE。我已经阅读了文档,但仍然不知道如何改进它。
提前感谢您的所有回复! :d
编辑:格式化
public static int charNumTrans(char toBeTranslated){
//Variable init
int translated = 0;
char guessedVariable;
//Checking if between a and i
if(toBeTranslated == 'a' || toBeTranslated == 'b' || toBeTranslated == 'c' || toBeTranslated == 'd' || toBeTranslated == 'e'
|| toBeTranslated == 'f' || toBeTranslated == 'g' || toBeTranslated == 'h' || toBeTranslated == 'i'){ //Checking to see which array to use
char[] dictionary;
dictionary = new char {'0','.','1','a','2','b','3','c','4','d','5','e','6','f','7','g','8','h','9','i'};
//chekcing between j and s
}else if(toBeTranslated == 'j' ||toBeTranslated == 'k' ||toBeTranslated == 'l' ||toBeTranslated == 'm' ||toBeTranslated == 'n' ||
toBeTranslated == 'o' ||toBeTranslated == 'p' ||toBeTranslated == 'q' ||toBeTranslated == 'r' || toBeTranslated == 's'){//Checking to see if in between
dictionary[10] = {'0','j','1','k','2','l','3','m','4','n','5','o','6','p','7','q','8','r','9','s'};
}else{//Everything else will be in this data set.
char[] dictionary = {'0','t','1','u','2','v','3','w','4','x','5','y','6','z'};
}
guessedVariable = dictionary[1];
while(dictionary[guessedVariable] != toBeTranslated){
guessedVariable +=2;
}
// Assigns letter minus one of array. e.g. b = dictionary[5]. This will then assign dictionary[4] to translated.
translated = Character.getNumericValue(dictionary[guessedVariable-1]);
return translated;
}
答案 0 :(得分:0)
更改 dictionary = new char {' 0','。',' 1',' a',' 2' ' b'&#39 3'' C'' 4'' d',' 5'' E'' 6'' F'&#39 7'' G&#39 ;, ' 8'' H'' 9'' I'};
到
dictionary = new char[] {'0','.','1','a','2','b','3','c','4','d','5','e','6','f','7','g','8','h','9','i'};
答案 1 :(得分:0)
对于数组声明,当您将值分配给您正在创建的数组对象时,您错过了[]
- 例如,将声明更改为:
char[] dictionary = new char[]{'0','t','1','u','2','v','3','w','4','x','5','y','6','z'};
另外,如果您尝试将字母转换为数字等值,我建议您将已传递给函数的char
转换为int
,然后减去61从这个价值?
原因是61对应于角色的位置' a '在Unicode字符表上,将极大地简化为传入的字母分配数字。
答案 2 :(得分:0)
首先,您没有在if语句之外初始化数组,这意味着您的代码最终无法调用"字典"阵列。其次,在场景中使用数组的问题是你有不同大小的数组。第三,正如人们所指出的那样,就初始化数组的方式而言,你需要创建一个适合你的数据集的新对象,例如: new char[] {...}
。
要完全解决您的问题,您可能需要考虑这样的事情(我已经使用相同的方法以最简单的方式初始化所有数组以避免混淆):
public static int charNumTrans(char toBeTranslated){
//Variable init
int translated = 0;
char guessedVariable;
//Checking if between a and i
if(toBeTranslated == 'a' || toBeTranslated == 'b' || toBeTranslated == 'c' || toBeTranslated == 'd' || toBeTranslated == 'e'
|| toBeTranslated == 'f' || toBeTranslated == 'g' || toBeTranslated == 'h' || toBeTranslated == 'i'){ //Checking to see which array to use
char[] dictionary = {'0','.','1','a','2','b','3','c','4','d','5','e','6','f','7','g','8','h','9','i'};
return findValue(dictionary);
//checking between j and s
}else if(toBeTranslated == 'j' ||toBeTranslated == 'k' ||toBeTranslated == 'l' ||toBeTranslated == 'm' ||toBeTranslated == 'n' ||
toBeTranslated == 'o' ||toBeTranslated == 'p' ||toBeTranslated == 'q' ||toBeTranslated == 'r' || toBeTranslated == 's'){//Checking to see if in between
char[] dictionary = {'0','j','1','k','2','l','3','m','4','n','5','o','6','p','7','q','8','r','9','s'};
return findValue(dictionary);
}else{//Everything else will be in this data set.
char[] dictionary = {'0','t','1','u','2','v','3','w','4','x','5','y','6','z'};
return findValue(dictionary);
}
}
public static int findValue(char[] dictionary){
guessedVariable = dictionary[1];
while(dictionary[guessedVariable] != toBeTranslated){
guessedVariable +=2;
}
// Assigns letter minus one of array. e.g. b = dictionary[5]. This will then assign dictionary[4] to translated.
translated = Character.getNumericValue(dictionary[guessedVariable-1]);
return translated;
}