前面我用switch确实是换汤不换药,写另一个程序是偶尔想到可以用Map集合来优化,把返回的状态码放在key中,信息放在value中,这样就可以通过Map的get方法获取状态信息。代码如下:- public String login(int a){
- int[] statusNum={200,400,404,409,460,461,462,463,464,465,471,472,474,475,476,477,479,480,481,482,483,484,485,486,487,488};
- String[] statusStr={"请求成功","错误的请求","没有找到访问页","登录冲突,请刷新页面再登录","请刷新页面再登录","请刷新页面再登录","请刷新页面再登录","无效的查询参数","缺失数据","访问错误次数较多,请5分钟后再登录!","无效的用户名","未找到用户名或密码不正确","验证码不正确","用户未登录系统","无效的加密锁","密码不正确","参数类型错误","参数错误","已登录系统","无效的跨域请求","请使用加密锁登录","用户状态未激活","活跃的用户","无效的用户","无效的用户","您的账户已锁定,请与管理员联系!"};
- String message="";
- Map<Integer,String> status=new HashMap<Integer,String>();
- for(int i=0;i<statusNum.length;i++){
- status.put(statusNum[i], statusStr[i]);
- }
- message=status.get(a);
- System.out.println(message);
- return message;
- }
- @Test
- public void testLogin(){
- login(400);
- }
复制代码
|