是否有更简洁的方法来执行此操作?
public void change(char x)
{
if(x == 'a')
{
return 'b';
}
else if(x == 'b')
{
return 'a';
}
else if(x == 't')
{
return 'r';
}
else if(x == 'r')
{
return 't';
}
return 'z';
}
我想知道 Java 中是否内置了任何东西,以便我可以像 Python 中的字典一样交换字符对,或者这是最好的方法吗?
请您参考如下方法:
可以尝试以下方法:
public char change(char x) {
final String dictionary = "abtr";
final String transform = "bart";
int loc= dictionary.indexOf(x);
if (loc < 0) {
return 'z';
} else {
return transform.charAt(loc);
}
}
