今天挑战的字符串转换成整数的程序

12 篇文章 0 订阅

今天挑战了下庞果网的编程挑战题,一个字符串转换成整数的程序,看了别人的代码,自己觉得很惭愧(我的代码真是又臭又长)

第一次上传代码时没有测试完全,导致有几个情况不合格,修改了下,基本满足题目要求

求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"));
	}

}

运行效果如图


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值