我正在尝试编写一个程序,它将字母表中的任何字母(大写或小写)和数字转换为拼音字母表。例如,如果我输入“A”或“a”,我的程序会给我(将其更改为)“Alpha”。此外,如果我输入“1”,它将返回“一”。我已经成功地处理了它的“输入任何字母”方面,但我的程序无法识别数字。我尝试输入int但我的扫描仪无法识别这一点。我在我的代码中设置了一个默认值,但仍然......没有占上风。我应该改用 if 语句吗?
进一步说明:这是这个问题的延续
这是我到目前为止所得到的:
import java.util.Scanner;
public class PhoneticTranslate {
public static void main(String[] args) {
int number = 0;
char letter;
String phonetic = null;
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a letter or number: ");
letter = kb.next().charAt(0);
switch(Character.toUpperCase(letter))
{
case 'A':
phonetic = "Alpha";
break;
case 'B':
phonetic = "Bravo";
break;
// ... rest of cases for letters
case 'Z':
phonetic = "Zulu";
break;
default:
Scanner x = new Scanner(System.in);
number = kb.nextInt();
switch(number)
{
case '1':
phonetic = "One";
break;
case '2':
phonetic = "Two";
break;
// ... rest of cases for numbers
case '8':
phonetic = "Eight";
break;
case '9':
phonetic = "Nine";
break;
}
}
System.out.println("You Entered " + letter + ". This letter indicates: " + phonetic);
System.out.println("You Entered" + number + ". This number indicates: " + phonetic);
}
}