for(String name: Server.list_name) if(name.equals(name_tokick)) { del(name); massage = "已将["+name+"]移出聊天室。"; break; }else { massage = "聊天室无此聊客。"; }为什么其中的if多次不成立却只输出一次else中的内容
时间: 2024-04-21 15:30:07 浏览: 20
在这段代码中,如果 `name_tokick` 不在 `Server.list_name` 中,那么 `if` 语句的条件将一直不成立,而 `else` 分支的语句会被执行。但是如果 `name_tokick` 在 `Server.list_name` 中,`if` 语句的条件成立,执行 `del(name)` 和 `massage = "已将["+name+"]移出聊天室。"`,然后使用 `break` 语句跳出整个循环,因此不会执行 `else` 分支语句。
因此,如果 `name_tokick` 在 `Server.list_name` 中,输出的是 "已将["+name+"]移出聊天室。",否则输出的是 "聊天室无此聊客。"。不会输出多次 `else` 分支语句,因为 `break` 语句会结束整个循环。
相关问题
帮我审查一下下带private Map<String, ItemsCustomTargetResult> getItemsCustomTargetResult(BidNodeViewDto bidNodeViewDto, Map<String, String> targetCodeMap) { Map<String, ItemsCustomTargetResult> itemsCustomTargetResultMap = new HashMap<>(); List<String> colName = new ArrayList<>(); colName.add(Constants.ProjectView.COL_NAME_EXPRESSION); colName.add(Constants.ProjectView.COL_NAME_EXPERSSION_VALUE); colName.add(Constants.ProjectView.COL_NAME_TOTAL); colName.add(Constants.ProjectView.COL_NAME_PERCENT); colName.add(Constants.ProjectView.COL_NAME_UNIT); for(String name : colName) { String value = null; if (Constants.ProjectView.COL_NAME_EXPRESSION.equals(name)) { value = bidNodeViewDto.getExpressionName() + ""; }else if(Constants.ProjectView.COL_NAME_EXPERSSION_VALUE.equals(name)) { value = bidNodeViewDto.getExpressionVal() + ""; }else if(Constants.ProjectView.COL_NAME_PERCENT.equals(name)) { value = bidNodeViewDto.getPercentTotal() + ""; }else if(Constants.ProjectView.COL_NAME_TOTAL.equals(name)) { value = bidNodeViewDto.getAmount() + ""; }else if(Constants.ProjectView.COL_NAME_UNIT.equals(name)) { value = bidNodeViewDto.getUnitIndex() + ""; } String targetCode = targetCodeMap.get(name); if(Strings.isBlank(targetCode)) { continue; } ItemsCustomTargetResult itemsCustomTargetResult = new ItemsCustomTargetResult(); itemsCustomTargetResultMap.put(targetCode,itemsCustomTargetResult.obtainItemsCustomTargetResult(targetCode, null, value, null)); } return itemsCustomTargetResultMap; }
这段代码的主要作用是根据传入的参数`bidNodeViewDto`和`targetCodeMap`生成一个`Map`,其中`Map`的键值为`targetCode`,值为`ItemsCustomTargetResult`对象。
这个方法看起来没有太大问题,但是有一些可以改进的地方:
1. 在循环中,每次迭代都会新建一个`String`类型的变量`value`并初始化为`null`,这个可以放到循环外部先声明再初始化,避免每次都进行声明和初始化。
2. `if...else`嵌套语句比较多,可以考虑使用`switch`语句来代替。
3. 在循环中,如果`targetCode`为空字符串,则直接跳过当前迭代,没有任何处理。这个可以添加一个日志输出或者抛出异常来提醒调用者传入的参数有误。
下面是一种改进后的代码:
```
private Map<String, ItemsCustomTargetResult> getItemsCustomTargetResult(BidNodeViewDto bidNodeViewDto, Map<String, String> targetCodeMap) {
Map<String, ItemsCustomTargetResult> itemsCustomTargetResultMap = new HashMap<>();
List<String> colName = Arrays.asList(Constants.ProjectView.COL_NAME_EXPRESSION, Constants.ProjectView.COL_NAME_EXPERSSION_VALUE, Constants.ProjectView.COL_NAME_TOTAL, Constants.ProjectView.COL_NAME_PERCENT, Constants.ProjectView.COL_NAME_UNIT);
for (String name : colName) {
String value = null;
switch (name) {
case Constants.ProjectView.COL_NAME_EXPRESSION:
value = String.valueOf(bidNodeViewDto.getExpressionName());
break;
case Constants.ProjectView.COL_NAME_EXPERSSION_VALUE:
value = String.valueOf(bidNodeViewDto.getExpressionVal());
break;
case Constants.ProjectView.COL_NAME_TOTAL:
value = String.valueOf(bidNodeViewDto.getAmount());
break;
case Constants.ProjectView.COL_NAME_PERCENT:
value = String.valueOf(bidNodeViewDto.getPercentTotal());
break;
case Constants.ProjectView.COL_NAME_UNIT:
value = String.valueOf(bidNodeViewDto.getUnitIndex());
break;
default:
throw new IllegalArgumentException("Invalid column name: " + name);
}
String targetCode = targetCodeMap.get(name);
if (targetCode == null || targetCode.isEmpty()) {
// 日志输出或者抛出异常
continue;
}
ItemsCustomTargetResult itemsCustomTargetResult = new ItemsCustomTargetResult();
itemsCustomTargetResultMap.put(targetCode, itemsCustomTargetResult.obtainItemsCustomTargetResult(targetCode, null, value, null));
}
return itemsCustomTargetResultMap;
}
```
public void addGfrBasAcctCheckset(GfrBasAcctChecksetDTO dto, Session session) { dto.setSet_year(String.valueOf(session.getBusiYear())); dto.setId(UUIDUtils.getUUID()); String report_item_code = dto.getReport_item_code(); String report_item_name = dto.getReport_item_name(); String other_report_item_code = dto.getOther_report_item_code(); String other_report_item_name = dto.getOther_report_item_name(); if (StringUtil.isNotBlank(report_item_code)) { if (",".equals(report_item_code.substring(0, 1))) { throw new BusinessException("请检查所选择的报表项目,父级不能和子级一起选择"); } String reportItemCode = StringUtils.getreportItemCode(report_item_code); dto.setReport_item_code(reportItemCode); } if (StringUtil.isNotBlank(report_item_name)) { if (",".equals(report_item_name.substring(0, 1))) { throw new BusinessException("请检查所选择的报表项目,父级不能和子级一起选择"); } String reportItemName = StringUtils.getreportItemName(report_item_name); dto.setReport_item_name(reportItemName); } gfrBasAcctChecksetDAO.addGfrBasAcctCheckset(dto); }
这段代码可以进行如下优化:
1. 将 `dto` 对象中的 `set_year` 和 `id` 设置移到对象创建时进行,避免多次重复设置。
2. 抽取相似的报表项目代码和报表项目名称的处理逻辑,避免代码重复。
3. 对于 `report_item_code` 和 `report_item_name` 的非空判断,可以使用 `StringUtils.isNotBlank()` 方法来进行判断。
优化后的代码如下:
```
public void addGfrBasAcctCheckset(GfrBasAcctChecksetDTO dto, Session session) {
String setYear = String.valueOf(session.getBusiYear());
String id = UUIDUtils.getUUID();
dto.setSet_year(setYear);
dto.setId(id);
String reportItemCode = handleReportItem(dto.getReport_item_code());
dto.setReport_item_code(reportItemCode);
String reportItemName = handleReportItem(dto.getReport_item_name());
dto.setReport_item_name(reportItemName);
gfrBasAcctChecksetDAO.addGfrBasAcctCheckset(dto);
}
private String handleReportItem(String reportItem) {
if (StringUtils.isNotBlank(reportItem)) {
if (",".equals(reportItem.substring(0, 1))) {
throw new BusinessException("请检查所选择的报表项目,父级不能和子级一起选择");
}
return StringUtils.getreportItemCode(reportItem);
}
return reportItem;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)