当前位置 : 主页 > 编程语言 > java >

java版atoi

来源:互联网 收集:自由互联 发布时间:2022-07-20
public class atoi { public static void main(String args[]) { char[] c = { '1', '0', '9', '4' }; int i = atoi(c); System.out.print(i); } private static int atoi(char[] c) { if (containsOthers(c)) return -1; int result = 0; for (int i = 0; i
public class atoi {
public static void main(String args[]) {
char[] c = { '1', '0', '9', '4' };
int i = atoi(c); System.out.print(i);
}
private static int atoi(char[] c) {
if (containsOthers(c))
return -1; int result = 0;
for (int i = 0; i < c.length; i++) { result = result + (c[i] - '0')
* (int) (Math.pow(10, c.length - i - 1)); }
return result;
// return c[2]-'0'+(c[1]-'0')*10+(c[0]-'0')*100; }
private static boolean containsOthers(char[] c) {
for (int i = 0; i < c.length; i++) {
if (c[i] - '0' < 0 || c[i] - '0' > 9)
return true;
}
return false;
}}
网友评论