我有点难过,并且一直试图解决这个问题.这是家庭作业,虽然我想学习编码而不管.在这里,我必须将用户输入的字符串转换为大写字母,然后使用电话键盘系统将那些大写字母转换为数字(2 = ABC等).
我已经做到这一点,但不确定我的下一步应该是什么.非常感谢任何想法,提前感谢.
package chapter_9;
import java.util.Scanner;
public class Nine_Seven {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a string: ");
String s = input.next();
// unsure what to do here, know i need some sort of output/return
// statement
}
public static int getNumber(char uppercaseLetter) {
String[] Keypad = new String[10];
Keypad[2] = "ABC";
Keypad[3] = "DEF";
Keypad[4] = "GHI";
Keypad[5] = "JKL";
Keypad[6] = "MNO";
Keypad[7] = "PQRS";
Keypad[8] = "TUV";
Keypad[9] = "WXYZ";
for (int i = 0; i < Keypad.length; i++) {
// unsure what to do here
}
return (uppercaseLetter);
}
}
Run Code Online (Sandbox Code Playgroud)
要获取char的数字,你应该反过来做你的数组.你的方法可能如下所示:
public static int getNumber(char uppercaseLetter) {
int[] keys = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
return keys[(int)uppercaseLetter - 65]; //65 is the code for 'A'
}
Run Code Online (Sandbox Code Playgroud)
将keys数组拉入类的成员变量也是一个好主意,这样您就不会在每次调用时初始化它.
至于输出/转换,我建议你看看java.lang.System课程.另请注意,您尚未将字符串转换为大写 - 并且未检查输入的有效性(它是仅由26个字母组成的字符串).