我怎样才能让这个 switch 语句使用扫描仪工作?
Posted
技术标签:
【中文标题】我怎样才能让这个 switch 语句使用扫描仪工作?【英文标题】:How can I get this switch statement to work using a scanner? 【发布时间】:2012-10-07 23:04:39 【问题描述】:我正在尝试编写一个程序,它将字母表中的任何字母(大写或小写)转换为 Phontic 字母表。例如,如果我输入“A”或“a”,我的程序会给我(将其更改为)“Alpha”。 我对这个和 switch 语句做了很多研究,但我一直卡住。我意识到我不能在扫描仪中使用“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;
【问题讨论】:
我宁愿维护一个 Mapphonetic
..您需要使用 charAt。 Scanner.next()
方法返回 String
而不是 char
所以你需要将 String
转换为 char
letter = kb.next().charAt(0);
【讨论】:
你速度很快,我写的也一样=)【参考方案2】:你可以更好地创建一个Map<Character, String>
来避免在 switch 中写26 cases
。这样,您只需 get
特定字符的字符串。
Map<Character, String> mapping = new HashMap<Character, String>();
mapping.put('a', "Alpha");
mapping.put('b', "Beta");
.. And so on..
当然你必须承担初始化Map
的负担,但它会比switch - case
的Mess
更好
好处是,你以后也可以从file
populate
Map
。
然后当您从扫描仪读取字符时,使用charAt(0)
获取第一个字符,因为Scanner.next()
返回一个字符串:-
letter = kb.next().charAt(0);
// Fetch the Phonetic for this character from `Map`
phonetic = mapping.get(letter);
【讨论】:
【参考方案3】: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);
【讨论】:
【参考方案4】:Scanner.next()
method 返回一个String
,而不是char
,因此您需要先使用String.charAt(...)
获取该String
的第一个字符,然后再将其与char
s 进行比较。
【讨论】:
以上是关于我怎样才能让这个 switch 语句使用扫描仪工作?的主要内容,如果未能解决你的问题,请参考以下文章