目标:我正在尝试根据用户的输入输出消息。
情况:基本上,用户将输入一些字符,例如5r(5红色)或8y(8黄色)。预期的表示法是第一个数字,然后是一个字符,但是用户可能不遵循该规则。
我使用了switch语句(这是练习的一部分),但是输出并没有给出我期望的结果
代码:
String input = reader.nextLine().toLowerCase();
String firstChar = input.substring(0,1);
String secondChar = input.substring(1);
String answer = "";
switch(firstChar)
{
case "0":
{
switch(secondChar)
{
case "b":
answer = "Zero Blue";
case "r":
answer = "Zero Red";
case "g":
answer = "Zero Green";
case "y":
answer = "Zero Yellow";
}
}
case "1":
{
switch(secondChar)
{
case "b":
answer = "One Blue";
case "r":
answer = "One Red";
case "g":
answer = "One Green";
case "y":
answer = "One Yellow";
}
}
... other case statements....
default:
answer = "not valid";
当用户写0b时,我的输出应为“零蓝色”,但当前显示为“无效”。为什么?
答案 0 :(得分:1)
如@markspace所述,您缺少一个break语句。以下是Java documentation关于switch语句的简短摘录:
每个break语句终止封闭的switch语句。控制流从切换块后的第一条语句继续。 break语句是必需的,因为没有它们,开关块中的语句就会掉落。
此外,我建议使用专用方法和for
循环以简化调试。
以下代码产生所需的结果:
public static String getAnswer() {
Scanner reader = new Scanner(System.in);
String input = reader.nextLine().toLowerCase();
String firstChar = input.substring(0, 1);
String secondChar = input.substring(1);
String[] chars = new String[] { "b", "r", "g", "y" };
String[] answers1 = new String[] { "Zero Blue", "Zero Red", "Zero Green", "Zero Yellow" };
String[] answers2 = new String[] { "One Blue", "One Red", "One Green", "One Yellow" };
if ((firstChar.equals("0"))) {
for (int i = 0; i < chars.length; i++) {
if (secondChar.equals(chars[i])) {
return answers1[i];
}
}
}
else if ((firstChar.equals("1"))) {
for (int i = 0; i < chars.length; i++) {
if (secondChar.equals(chars[i])) {
return answers2[i];
}
}
}
return "not valid";
}
public static void main(String[] args) throws IOException {
System.out.println(getAnswer());
}
编辑:除了上述解决方案之外,我还想提供一种更复杂的解决方案,其中涉及枚举器的使用。它应该使您的代码更干净,更有意义并且模块化程度更高:
public enum Answer {
BLUE('b',"Blue"), RED('r',"Red"),
GREEN('g',"Green"), YELLOW('y',"Yellow");
private static final String[] prefix = new String[] {"Zero", "One"};
char letter;
String name;
Answer(char letter, String name) {
this.letter = letter;
this.name = name;
}
public static String getForInput(char input, int index) {
if (index > prefix.length)
return "value out of range " + index;
for (Answer answer : Answer.values()) {
if (input == answer.letter)
return prefix[index] + " " + answer.name;
}
return "unable to find answer for input " + input;
}
}
public static String getAnswer() {
System.out.println("Enter your input: ");
Scanner reader = new Scanner(System.in);
String input = reader.nextLine().toLowerCase();
int number = Integer.valueOf(input.substring(0, 1));
char letter = input.substring(1).charAt(0);
return Answer.getForInput(letter, number);
}
答案 1 :(得分:1)
我建议完全不使用夹心大小写,请使用哈希映射:
package mypackage;
import java.util.*;
public class MyComputingClass {
private static Map<Character, String> firstCharMapping;
private static Map<Character, String> secondCharMapping;
public static void main(String[] args) {
initCharMappers();
System.out.println("Mapping of 0Y : " + mapStuff("0Y"));
System.out.println("Mapping of 0y : " + mapStuff("0y"));
System.out.println("Mapping of 0YJ : " + mapStuff("0YJ"));
System.out.println("Mapping of 2B : " + mapStuff("2B"));
System.out.println("Mapping of eB : " + mapStuff("eB"));
}
private static void initCharMappers() {
firstCharMapping = new HashMap<>();
secondCharMapping = new TreeMap<>(Comparator.comparing(Character::toLowerCase));
firstCharMapping.put('0', "Zero");
firstCharMapping.put('1', "One");
firstCharMapping.put('2', "Two");
secondCharMapping.put('Y', "Yellow");
secondCharMapping.put('B', "Blue");
secondCharMapping.put('G', "Green");
}
private static String mapStuff(String str) {
String INVALID_INPUT = "Invalid input";
if (!str.matches("\\d[a-zA-Z]")) {
return INVALID_INPUT;
}
Character firstChar = str.toCharArray()[0];
Character secondChar = str.toCharArray()[1];
if (!firstCharMapping.containsKey(firstChar) || !secondCharMapping.containsKey(secondChar)) {
return INVALID_INPUT;
}
return firstCharMapping.get(firstChar) + " " + secondCharMapping.get(secondChar);
}
}
输出:
Mapping of 0Y : Zero Yellow
Mapping of 0y : Zero Yellow
Mapping of 0YJ : Invalid input
Mapping of 2B : Two Blue
Mapping of eB : Invalid input