【发布时间】:2017-02-12 18:35:41
【问题描述】:
我正在尝试将 String 转换为 ascii,更改 ascii 值,然后将这些值转换回字符串。我虽然走在正确的轨道上,但我收到一条错误消息,告诉我必须返回一个字符串;我哪里做错了?
public static boolean safeToUse(String text) {
text = text.toUpperCase();
int length = text.length();
for ( int a=0; a < length; a++ ) {
char c = text.charAt(a);
if (c < FIRST || c > LAST) { //checking range
return false;
}
}
return true;
}
public static String rot31(String message)
{
message = message.toUpperCase();
int length = message.length();
for ( int x=0; x < length; x++ ) {
int ch = message.charAt(x);
if (ch <= 62) {
int ascii = ch + 31;
} else {
int ascii = ch - 62;
String coded = Integer.toString(ascii);
return coded;
}
}
}
【问题讨论】:
-
提示:
rot31(String)以""的形式返回一个空的String是什么?另外我认为你误解了return所做的事情,因为我认为你不想在第一个char出现大于62时返回 -
如果字符串中没有字符的 ASCII 值高于 62,
rot31可能永远不会返回。我认为编译器正在抱怨这一点。