在下面的代码中,我要获取IP,并根据命令将GET请求返回。一些GET请求具有修饰符,这些修饰符是我可以通过另一个GET请求对相同URL获得的数字。确保这些修饰符有效以使程序不会挂起的好方法是什么?
public class UrlTanslatorInstance {
public String ip;
public UrlTanslatorInstance(String ip){
this.ip = ip;
}
public String translate(String command, String modifier) throws MalformedURLException, IOException{
String url;
switch (command.toUpperCase()){
case "LEFT":
return "http://" + ip + "keypress/left";
case "RIGHT":
return "http://" + ip + "keypress/right";
case "KEYUP":
return "http://" + ip + "keyup/" + modifier;
case "KEYDOWN":
return "http://" + ip + "keydown/" + modifier;
case "KEYPRESS":
return "http://" + ip + "keypress/" + modifier;
case "APPIDS":
return "http://" + ip + "query/apps";
case "LAUNCH":
return "http://" + ip + "keypress/left";
case "ACTIVEAPP":
return "http://" + ip + "query/active-app";
}
return "Command Not Valid";
}
}
最佳答案
您的意思是您如何处理未定义的案例?
使用default关键字处理未定义的案例。
switch (foo){
case a:
doSomething();
break;
case b:
doSomethingElse();
break;
default:
handleUndefinedCases();
break;
}
关于java - 控制台应用程序中的错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41399081/