今天挑战的字符串转换成整数的程序
来源:互联网 发布:mysql 删除字段 编辑:程序博客网 时间:2024/06/04 05:29
今天挑战了下庞果网的编程挑战题,一个字符串转换成整数的程序,看了别人的代码,自己觉得很惭愧(我的代码真是又臭又长)
第一次上传代码时没有测试完全,导致有几个情况不合格,修改了下,基本满足题目要求
求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"));}}
运行效果如图
- 今天挑战的字符串转换成整数的程序
- 关于把字符串整数转换成整数的程序
- 整数转换成字符串的程序--不太成熟
- 整数转换成16进制字符串的程序
- 挑战题:字符串转换成整数 “456”转换456
- 整数字符串的转换
- 挑战面试编程:字符串转换为整数
- 编写程序实现字符串到整数的转换。
- 整数转换成固定位数的字符串
- 整数/字符串之间的转换
- 字符串转换为整数(atoi)与整数转换成字符串(itoa)的实现
- 算法:将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。
- 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数
- 字符串转换成整数,通配符的字符串匹配问题
- 字符串转换成整数/带通配符的字符串匹配
- 字符串转换成整数&带通配符的字符串匹配问题
- 字符串转换成整数,带通配符的字符串匹配
- 计蒜客挑战难题:整数转换成罗马数字
- 类库JSONCPP之二-JSON及JSONCPP介绍
- [转载]mysql 数据库导入\导出
- 栈的简单实现(三)
- make版本管理器
- Servlet.service() for servlet jsp threw exception
- 今天挑战的字符串转换成整数的程序
- 在stutst2里面的action得到内置对象的方法
- css position 属性 定位学习
- sizeof&指针
- Source Insight完美转换UTF-8 到 GB2312
- B树的一些总结(另外还有B+数B-树B*树)
- HTTP协议详解
- 黑马程序员_java学习笔记
- 程序员该有的艺术气质—SOLID原则