今天挑战了下庞果网的编程挑战题,一个字符串转换成整数的程序,看了别人的代码,自己觉得很惭愧(我的代码真是又臭又长)
第一次上传代码时没有测试完全,导致有几个情况不合格,修改了下,基本满足题目要求
求July能指点一二
package ex25;
public class GlobalMembers {
public static int StrToInt(String str){
int temp;
char digit;
char firstOne = 0;
int result[] = new int[]{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
boolean flag = true; // 查找第一个字符的标志
boolean isFirstDigit = true; // 确认数字第一位的标志
int num = 0;
int j = 0;
if(str.length() == 0){
return 0;
}
// 获得整数字符串找出符号位
for(int i = 0; i < str.length(); i++){
digit = str.charAt(i);
temp = digit - '0';
if(((temp >= 0 && temp <= 9) || (digit == '-' || digit == '+')) && flag){
firstOne = digit;
if(firstOne != '-' && firstOne != '+'){
result[0] = temp;
}
flag = false;
continue;
}else if(digit == ' ' && firstOne == 0){
continue;
}
else if(flag){
return 0;
}
if(isFirstDigit){
if(firstOne - '0' >= 0 && firstOne - '0' <= 9){
j = 1;
}else if(firstOne == '-' || firstOne == '+'){
j = 0;
}
isFirstDigit = false;
}
if(temp >= 0 && temp <= 9){
result[j] = temp;
j++;
}else{
break;
}
}
// 得出最后的整数
int k = 0;
while(result[k] != -1){
num += result[k];
k++;
if(result[k] != -1)
num *= 10;
}
if(firstOne != '-'){
if(num < 0)
return Integer.MAX_VALUE;
else
return num;
}
else{
if(-1 * num > 0)
return Integer.MIN_VALUE;
else
return -1 * num;
}
}
public static void main(String[] args) {
// TODO 自动生成方法存根
//Scanner sc = new Scanner(System.in);
//System.out.println("Enter a number: ");
//String s = sc.next();
System.out.println("The number is " + StrToInt(""));
System.out.println("The number is " + StrToInt("1"));
System.out.println("The number is " + StrToInt("+1"));
System.out.println("The number is " + StrToInt("-1"));
System.out.println("The number is " + StrToInt("123"));
System.out.println("The number is " + StrToInt("-123"));
System.out.println("The number is " + StrToInt("010"));
System.out.println("The number is " + StrToInt("+00131204"));
System.out.println("The number is " + StrToInt("-01324000"));
System.out.println("The number is " + StrToInt("2147483647"));
System.out.println("The number is " + StrToInt("-2147483647"));
System.out.println("The number is " + StrToInt("-2147483648"));
System.out.println("The number is " + StrToInt("2147483648"));
System.out.println("The number is " + StrToInt("-2147483649"));
System.out.println("The number is " + StrToInt("abc"));
System.out.println("The number is " + StrToInt("-abc"));
System.out.println("The number is " + StrToInt("1a"));
System.out.println("The number is " + StrToInt("23a8f"));
System.out.println("The number is " + StrToInt("-3924x8fc"));
System.out.println("The number is " + StrToInt(" 321"));
System.out.println("The number is " + StrToInt(" -321"));
System.out.println("The number is " + StrToInt("123 456"));
System.out.println("The number is " + StrToInt("123 "));
System.out.println("The number is " + StrToInt(" - 321"));
System.out.println("The number is " + StrToInt(" +4488"));
System.out.println("The number is " + StrToInt(" + 413"));
System.out.println("The number is " + StrToInt(" ++c"));
System.out.println("The number is " + StrToInt(" ++1"));
System.out.println("The number is " + StrToInt(" --2"));
System.out.println("The number is " + StrToInt(" -2"));
}
}
运行效果如图