我正在尝试编写一个程序,将字母表中的任何字母(大写或小写)和数字切换到拼音字母表中。例如,如果我输入“A”或“a”,我的程序将给我(更改为)“Alpha”。此外,如果我输入“1”,它将返回“一”。我已成功设法完成它的“输入任意字母”方面,但我的程序无法识别数字。我尝试了 int ,但我的扫描仪无法识别这一点。我在我的代码中添加了默认,但仍然没有优先权。我应该使用if语句吗?
进一步说明: 这个问题是此question
的延续这是我到目前为止所得到的:
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);
}
}
答案 0 :(得分:4)
巨型开关/案例条款是一种代码味道,试试这个:
将每个键/值对添加到Map中,然后使用get
检索值。无需开关/箱。
String letter;
String phonetic;
Map<String,String> codes = new HashMap<String,String>();
codes.put("A","Alpha");
codes.put("B","Bravo");
codes.put("C","Charlie");
codes.put("D","Delta");
// not showing all "puts" to make it shorter
codes.put("W","Whiskey");
codes.put("X","X-Ray");
codes.put("Y","Yankee");
codes.put("Z","Zulu");
codes.put("0","Zero");
codes.put("1","One");
// not showing all "puts" to make it shorter
codes.put("9","Nine");
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a letter: ");
letter = kb.next().toUpperCase(); // convert key to uppercase
phonetic = codes.get(letter); // search the value in the map using the key
if (phonetic == null) {
System.out.println("bad code : " + letter);
} else {
System.out.println("Phonetic: " + phonetic);
}
答案 1 :(得分:3)
您已将案件写入字符: -
case '1': // This is checking for character '1'
您需要更改案例以获取integer
值: -
switch(number) {
case 1:
phonetic = "One";
break;
case 2:
... so on
答案 2 :(得分:0)
要么不包括数字周围的引号(“case 1:phonetic =”One“”etc),要么继续使用char值。我想其中任何一个都应该有用。
答案 3 :(得分:0)
您的switch语句正在检查整数的unicode char表示。根据此规范,'1'
是字符“1”,它转换为整数49。
将每个值的int表示形式放在switch语句中:
switch (number) {
case 1:
phonetic = "One";
break;
case 2:
...
}
答案 4 :(得分:0)
尝试:
Scanner x = new Scanner(System.in);
int number = x.nextInt();
String phonetic = null;
switch(number)
{
case 1:
phonetic = "One";
break;
case 2:
phonetic = "Two";
break;
case 3:
phonetic = "Three";
break;
case 4:
phonetic = "Four";
break;
case 5:
phonetic = "Five";
break;
case 6:
phonetic = "Six";
break;
case 7:
phonetic = "Seven";
break;
case 8:
phonetic = "Eight";
break;
case 9:
phonetic = "Nine";
break;
}
答案 5 :(得分:0)
使用ASCII码代替数字,这就是字符无论如何。但为什么你需要这样做呢?你的代码是否已经有效?
答案 6 :(得分:0)
将您的案例继续使用代表整数的字符:
case 'Z':
phonetic = "Zulu";
break;
case '1':
phonetic = "One";
break;
case '2':
// ...
只要你只想处理一位数字,这就行了。
这与您的问题描述相符,但同时保留letter
和number
变量并单独打印它们会显示一些其他功能吗?
答案 7 :(得分:0)
您可以尝试检查输入值是否为数字(int)。如果没有返回
while(true){
Scanner x = new Scanner(System.in);
int number=0;
try{
int number = x.nextInt();
}catch(IllegalArgumentException e){
continue;
}
String phonetic = null;
switch(number)
{
case 1:
phonetic = "One";
break;
case 2:
phonetic = "Two";
break;
case 3:
phonetic = "Three";
break;
case 4:
phonetic = "Four";
break;
case 5:
phonetic = "Five";
break;
case 6:
phonetic = "Six";
break;
case 7:
phonetic = "Seven";
break;
case 8:
phonetic = "Eight";
break;
case 9:
phonetic = "Nine";
break;
}
}
答案 8 :(得分:0)
试试这个先生
package phone;
import java.util.Scanner;
public class PhoneticTranslate {
/**
* @param args
*/
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);
}
}