public class Test{
public static long solution(String str)throws Exception {
if(strnull||str.length()0)throw new Exception(“输入数据有误”);
long num=0;
boolean minus=false;
int index=0;
if(str.charAt(index)’+’) {
index++;
}else if(str.charAt(index)’-’){
index++;
minus=true;
}
int flag=minus?-1:1;
if(index<str.length()) {
while(index<str.length()) {
if(str.charAt(index)>='0'&&str.charAt(index)<='9') {
int digit=str.charAt(index)-'0';
num=num*10+digit;
index++;
if(num>Integer.MAX_VALUE) {
throw new Exception("输入数据超过范围");
}
}else {
throw new Exception("输入数据有误");
}
}
}else {
throw new Exception("输入数据有误");
}
return num*flag;
}
public static void main(String[] args) throws Exception {
System.out.println(solution("-12345"));
}
}