0

我正在尝试编写一个程序,它将字母表中的任何字母(大写或小写)和数字转换为拼音字母表。例如,如果我输入“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);


}
}
4

9 回答 9

4

一个巨大的 switch/case 子句是一种代码味道,试试这个:

将每个键/值对添加到 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);
}
于 2012-10-18T15:34:14.790 回答
3

您已将案例写在字符上:-

case '1': // This is checking for character '1'

您需要更改案例以获取integer值:-

switch(number) {
    case 1:
           phonetic = "One";
           break;
    case 2:
        ... so on
于 2012-10-18T15:30:50.603 回答
0

您可以尝试检查输入值是否为数字(整数)。如果不返回

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;
    }
}
于 2012-10-18T15:45:04.677 回答
0

要么不要在数字周围加上引号(“ case 1: phonetic = "One"" 等),要么继续使用 char 值。我认为任何一个都应该工作。

于 2012-10-18T15:30:58.777 回答
0

您的 switch 语句正在检查整数的 unicode char 表示形式。根据这个规范,'1'是字符“1”,它转换为整数 49。

将每个值的 int 表示形式放在 switch 语句中:

switch (number) {
    case 1:
        phonetic = "One";
        break;
    case 2:
    ...
}
于 2012-10-18T15:31:31.163 回答
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;
    }
于 2012-10-18T15:32:14.517 回答
0

使用 ASCII 码代替数字,这就是字符。但是为什么你需要这样做呢?你的代码不是已经工作了吗?

于 2012-10-18T15:32:42.487 回答
0

将您的案例继续到表示整数的字符中:

case 'Z':
    phonetic = "Zulu";
    break;
case '1':
    phonetic = "One";
    break;
case '2':
    // ...

只要您只想处理一位数,这将起作用。

这与您的问题描述相匹配,尽管同时保留 aletternumber变量并将它们分别打印出来暗示了一些额外的功能?

于 2012-10-18T15:32:51.677 回答
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);


 }

}
于 2017-02-07T12:24:35.130 回答