【发布时间】:2016-01-15 19:40:54
【问题描述】:
我正在尝试做一些错误捕获。
错误应该检查数组的长度是否小于2,并检查HashMap是否包含用户输入的键。
捕获的错误必须仅使用 if 语句,并且必须使用 .length() 方法,并且必须使用 HashMap java API 中的 .get() 方法。
我已经像这样声明了 HashMap:
private HashMap <String, Shape> shapes;
下面是我的其余代码。
String basicCommand = commands[0];
String moreCommands = commands[1];
int size = commands.length;
if(size < 2 && shapes.get(commands[1]).equals(commands[0])){
if(basicCommand.equals("circle")) {
makeACircle(commands);
}
else if(basicCommand.equals("help")) {
printHelp();
}
else if(moreCommands.equals("left")){
moveLeft(commands);
}
else if(moreCommands.equals("down")){
shapes.get(commands[0]).moveDown();
}
else if(moreCommands.equals("up")){
shapes.get(commands[0]).moveUp();
}
else if(moreCommands.equals("right")){
shapes.get(commands[0]).moveRight();
}
else if(moreCommands.equals("visible")){
shapes.get(commands[0]).makeVisible();
}
else if(moreCommands.equals("invisible")){
shapes.get(commands[0]).makeInvisible();
}
else if(commands[0].equals("forget")){
shapes.remove(commands[1]).makeInvisible();
}
else {
System.out.println("Unknown command: " + basicCommand);
}
}
else{
System.out.println("error");
}
}
【问题讨论】:
标签: java arrays error-handling hashmap logic