java 如何使用扫描仪使这个 switch 语句起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12938563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get this switch statement to work using a scanner?
提问by user1753668
I'm trying to write a program that will switch any letter of the alphabet (upper or lower cases) into the Phontic alphabet. For example, If I enter "A" or "a" my program will give me (change it to) "Alpha". I've done so much research on this and switch statements but I keep getting stuck. I've realized that I can't use 'char' in a scanner. However, when I change 'char' into a 'String' my switch statement messes up (specifically the toUpperCasein my code gets underlined. I can't see my mistake. Here's what I've done so far:
我正在尝试编写一个程序,可以将字母表中的任何字母(大写或小写)转换为 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;
}
}
}
回答by Amit Deshpande
You need to use charAt. Scanner.next()
method returns String
not char
so you will need to convert String
to char
您需要使用 charAt。Scanner.next()
方法String
不返回char
所以你需要转换String
为char
letter = kb.next().charAt(0);
回答by Rohit Jain
You can better create a Map<Character, String>
to save yourself from writing 26 cases
in switch. This way you just have to get
the String for a particular character.
您可以更好地创建一个Map<Character, String>
以防止自己26 cases
在 switch 中写入。这样你只需要get
为一个特定的字符字符串。
Map<Character, String> mapping = new HashMap<Character, String>();
mapping.put('a', "Alpha");
mapping.put('b', "Beta");
.. And so on..
Of course you have to take the burden of initializing the Map
, but it will be better than a Mess
of switch - case
当然,你必须要初始化的负担Map
,但它会比一个更好Mess
的switch - case
Benefit is that, you can also populate
the Map
from some file
later on.
好处是,你还可以populate
将Map
一些file
以后。
Then when you read character from scanner, use charAt(0)
to fetch the first character, because Scanner.next()
returns a String: -
然后当您从扫描仪读取字符时,用于charAt(0)
获取第一个字符,因为Scanner.next()
返回一个字符串:-
letter = kb.next().charAt(0);
// Fetch the Phonetic for this character from `Map`
phonetic = mapping.get(letter);
回答by Tulains Córdova
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);
}
回答by Richard JP Le Guen
The Scanner.next()
methodreturns a String
, not a char
, so you need to get the first character of that String
using String.charAt(...)
before comparing it to char
s.
该Scanner.next()
方法返回 a String
,而不是 a char
,因此您需要在将其与s进行比较之前获取String
using的第一个字符。String.charAt(...)
char