如何使用扫描仪使此switch语句正常工作?

时间:2012-10-17 16:06:58

标签: java string switch-statement java.util.scanner phonetics

我正在尝试编写一个程序,将字母表中的任何字母(大写或小写)切换为Phontic字母表。例如,如果我输入“A”或“a”,我的程序将给我(更改为)“Alpha”。 我已经对这个和切换语句做了很多研究,但我一直陷入困境。我意识到我不能在扫描仪中使用'char'。但是,当我将'char'更改为'String'时,我的switch语句会混乱(特别是我的代码中的 toUpperCase 会被加下划线。我无法看到我的错误。这就是我所做的事情。远:

import java.util.Scanner;
public class PhoneticTranslate {
public static void main(String[] args) {


char letter;
String phonetic;

Scanner kb = new Scanner(System.in);


System.out.print("Please enter a letter: ");
letter = kb.next();

switch(Character.toUpperCase(letter))
{
case 'A':
    phonetic = "Alpha";
break;
case 'B':
    phonetic = "Bravo";
    break;
case 'C':
    phonetic = "Charlie";
    break;
case 'D': 
    phonetic = "Delta";
    break;
case 'E':
    phonetic = "Echo";
    break;
case 'F':
    phonetic = "Foxtrot";
    break;
case 'G':
    phonetic = "Golf";
    break;
case 'H':
    phonetic = "Hotel";
    break;
case 'I':
    phonetic = "India";
    break;
case 'J':
    phonetic = "Juliet";
case 'K':
    phonetic = "Kilo";
    break;
case 'L':
    phonetic = "Lima";
    break;
case 'M':
    phonetic = "Mike";
    break;
case 'N':
    phonetic = "November";
    break;
case 'O':
    phonetic = "Oscar";
    break;
case 'P':
    phonetic = "Papa";
    break;
case 'Q':
    phonetic = "Quebec";
    break;
case 'R':
    phonetic = "Romeo";
    break;
case 'S':
    phonetic = "Sierra";
    break;
case 'T':
    phonetic = "Tango";
    break;
case 'U':
    phonetic = "Uniform";
    break;
case 'V':
    phonetic = "Victor";
    break;
case 'W':
    phonetic = "Whiskey";
    break;
case 'X':
    phonetic = "X-Ray";
    break;
case 'Y':
    phonetic = "Yankee";
    break;
case 'Z':
    phonetic = "Zulu";
    break;


}

}
}

4 个答案:

答案 0 :(得分:6)

您需要使用charAt。 Scanner.next()方法返回String而不是char,因此您需要将String转换为char

letter = kb.next().charAt(0);

答案 1 :(得分:3)

您可以更好地创建Map<Character, String>以避免在交换机中编写26 cases。这样你只需要get特定字符的字符串。

Map<Character, String> mapping = new HashMap<Character, String>();
mapping.put('a', "Alpha");
mapping.put('b', "Beta");
..  And so on..

当然,你必须承担初始化Map的责任,但它会优于Mess的{​​{1}}

好处是,您稍后也可以switch - case populateMap

然后当你从扫描仪中读取字符时,使用file来获取第一个字符,因为charAt(0)返回一个字符串: -

Scanner.next()

答案 2 :(得分:1)

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 assignments to make it shorter
codes.put("W","Whiskey");
codes.put("X","X-Ray");
codes.put("Y","Yankee");
codes.put("Z","Zulu");

Scanner kb = new Scanner(System.in);

System.out.print("Please enter a letter: ");
letter = kb.next().toUpperCase();

phonetic = codes.get(letter);

if (phonetic == null) {
    System.out.println("bad code : " + letter);
} else {
    System.out.println("Phonetic: " + phonetic);
}

答案 3 :(得分:0)

Scanner.next() method返回String而不是char,因此您需要先使用String.charAt(...)获取String的第一个字符,然后再将其与char进行比较{{1}}秒。