Java 字符数组错误
Java Character Array Errors
我正在制作一种将字符翻译成相应数字的方法,例如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;
}
改变
dictionary = new char {'0','.','1','a','2','b','3','c','4','d','5','e','6','f','7','g','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'};
对于您的数组声明,当您将值分配给您正在创建的数组对象时,您缺少 []
- 因此,例如,将您的声明更改为:
char[] dictionary = new char[]{'0','t','1','u','2','v','3','w','4','x','5','y','6','z'};
此外,如果您尝试将字母转换为等价的数字,我建议您将传递给函数的 char
转换为 int
,然后减去 61从这个值?
原因是 61 对应于字符 'a' 在 Unicode 字符 table 上的位置,并且会大大简化为您传入的字母分配数字的过程。
首先,您没有在 if 语句之外初始化数组,这意味着最后的代码将无法调用 "dictionary" 数组。其次,在您的场景中使用数组的问题是您有不同大小的数组。第三,正如人们所指出的,就如何初始化数组而言,您需要创建一个适合您的数据集的新对象,例如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;
}
我正在制作一种将字符翻译成相应数字的方法,例如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;
}
改变 dictionary = new char {'0','.','1','a','2','b','3','c','4','d','5','e','6','f','7','g','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'};
对于您的数组声明,当您将值分配给您正在创建的数组对象时,您缺少 []
- 因此,例如,将您的声明更改为:
char[] dictionary = new char[]{'0','t','1','u','2','v','3','w','4','x','5','y','6','z'};
此外,如果您尝试将字母转换为等价的数字,我建议您将传递给函数的 char
转换为 int
,然后减去 61从这个值?
原因是 61 对应于字符 'a' 在 Unicode 字符 table 上的位置,并且会大大简化为您传入的字母分配数字的过程。
首先,您没有在 if 语句之外初始化数组,这意味着最后的代码将无法调用 "dictionary" 数组。其次,在您的场景中使用数组的问题是您有不同大小的数组。第三,正如人们所指出的,就如何初始化数组而言,您需要创建一个适合您的数据集的新对象,例如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;
}