问题:
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.com.picc.flowable.draw.rest;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.flowable.idm.api.IdmIdentityService;
import org.flowable.ui.common.model.GroupRepresentation;
import org.flowable.ui.common.model.RemoteGroup;
import org.flowable.ui.common.model.ResultListDataRepresentation;
import org.flowable.ui.common.service.idm.RemoteIdmService;
import org.flowable.ui.idm.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Rest resource for managing groups, used in the editor app.
*/
public class EditorGroupsResource {
public static void main(String[] args) {
List<String> scList = new ArrayList<>();
Scanner sc = new Scanner(System.in);
boolean scFlag = true;
System.out.println("开始输入(输入end结束)");
// 1、循环把输入的值存到集合中
while(scFlag) {
String str = sc.nextLine();
scList.add(str);
// 2、结束循环的条件
if ("end".equals(str) || scList.size() >= 100){
scFlag = false;
}
}
// 输出结果
System.out.println(sum(scList));
}
/**
* 计算结果
* @param scList
* @return
*/
private static HashMap<String, Integer> sum(List<String> scList){
HashMap<String, Integer> hashMap = new HashMap<>();
// 循环控制标志
boolean flag = true;
// 集合的长度
int size = scList.size();
// 记录集合剩余长度
int count = size;
while (flag) {
String str = scList.get(size - count);
String[] indexStr = str.split(" ");
switch (indexStr[0]) {
case "mov": {
// 说明是声明变量
if (isInteger(indexStr[2])) {
hashMap.put(indexStr[1], Integer.valueOf(indexStr[2]));
}else {
// 当下标为2的不是数值类型,就应该是 mov bx ax类型
String[] indexOne = scList.get(0).split(" ");
hashMap.put(indexStr[1], hashMap.get(indexOne[1]));
}
count--;
break;
}
case "dec": {
// 做减法
Integer num = hashMap.get(indexStr[1]);
hashMap.put(indexStr[1], num - 1);
count--;
break;
}
case "inc": {
// 做加法
Integer num = hashMap.get(indexStr[1]);
hashMap.put(indexStr[1], num + 1);
count--;
break;
}
case "jnz": {
// 跳转指令
Integer num = hashMap.get(indexStr[1]);
if (num == 0){
count--;
}else {
count = count + Math.abs(Integer.valueOf(indexStr[2]));
}
break;
}
default:
break;
}
if ("end".equals(indexStr[0])) {
flag = false;
}
}
return hashMap;
}
/**
* 判断字符串中是否为数字
*
* @param str
* @return
*/
private static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^-?[0-9]+");
return pattern.matcher(str).matches();
}
}