我正在尝试编写一个允许我将输入的字母转换为数字的函数,反之亦然,到目前为止,我一直在编译错误。我想保持这个小而有效,而不必做一个广泛的if,else if语句。
到目前为止,这是我的代码:
public class CaesarCipher {
/*
* create function that converts a letter to a number
* ex. a -> 0, b -> 1, etc...
*/
static char letterToNumber (char firstLetter){
if (firstLetter < 'a' || firstLetter > 'z') {
throw new IllegalArgumentException("Only lower-case ASCII letters are valid");
}
return (char) (firstLetter - 'a');
}
/*
* function to allow a user input a number and converts to a letter
* 0->a and 1->b, etc...
*/
static int numberToLetter (int firstNumer){
if (firstNumber < '0' || firstNumber > '25'){
}
return firstNumber;
}
public static void main(String[] args) {
char a = 0;
// TODO Auto-generated method stub
System.out.println (letterToNumber (a)); //suppose to compile to convert a -> the number 0
System.out.println(numberToLetter (1)); //compile to convert 1 -> the letter b
}
}
答案 0 :(得分:0)
你缺少&#39; a&#39; +在numberToLetter的return语句中
return 'a' + firstNumber;