我只想将小写字母减为小写字母.我通过获取字符的ASCII值并将其递减来实现.但是,例如,如果我将a减2,答案应该是y.不是符号或大写字母.
int charValue = temps.charAt(i);
String increment = String.valueOf( (char) (charValue - (m) ));
if((charValue - m) < 65){
int diff = 65 - (charValue - m);
increment = String.valueOf((char) (91 - diff));
}else if((charValue - m) < 97 && (charValue - m) >= 91){
int diff = 97 - (charValue - m);
increment = String.valueOf((char) (123 - diff));
}
System.out.print(increment);
这是我到目前为止的代码.问题是如果我将a减8,它会显示一个大写字母.
例如:-如果我输入“ a”且m值为8,则预期输出应为“ s”.但是我得到“ Y”
解决方法:
这里的charToBeChanged是要转换的小写字符. decrementValue是要移动多少的值.在主要帖子中,您说:
if i input ‘a’ and m value as 8, the expected output should be ‘s’
因此,这里的charToBeChanged是a,而decrementValue是8.
System.out.println((char) ((charToBeChanged - 'a' + 26 - decrementValue) % 26 + 'a'));