挑错+修改(不能一次全想明白,哭),考虑的情况有点多
大概分为
1.首位为符号位
2.'e'和'.'的数量只能小于等于1
3.'e'后面必须还有其他值
4.'e'后面可接'+','-'号,
5.'e'后面的字符串中不能出现'.'
6.str[i]在['0'~'9']之间
public class Solution { public static boolean isNumeric(char[] str) { if(str.length==0) { return false; } int i=0; int e_num = 1;//'e'的最大数量 int point_num =1;//'.'的最大数量 if(str[0]=='-'||str[0]=='+') { if(str.length==1) { return false;//后面没有值,只有符号; } i=1;//到时候循环的时候从什么时候开始, } for(;i<str.length;i++) { if(e_num<0||point_num<0) {//.和e只能有一个 return false; } if(str[i]=='e'||str[i]=='E') {//e if(i==str.length-1) {// return false; } int k=i+1; for(;k<str.length;k++) {//出现e以后,后面的字符中不能出现小数点 if(str[k]=='.') { return false; } } if(str[i+1]=='-'||str[i+1]=='+') {//匹配成功 i++; continue; } }else if(str[i]=='.') { point_num-=1; }else if(str[i]<'0'||str[i]>'9') { return false; } } return true; } }