【发布时间】:2015-02-20 13:40:52
【问题描述】:
我想将一个数字作为字符串读取,并将其字符拆分为一个整数数组,并通过循环遍历该整数数组来找到它的数字之和。
这是我目前的代码:
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
String Number = S.next();
int counterEnd = Number.length();
int sum = 0 ;
for ( int i = 0 ; i < counterEnd ; i++) {
sum += sum + (Number.charAt(i));
}
System.out.println(sum);
}
不幸的是,这段代码打印的是 ASCII 的总和而不是数字。
【问题讨论】:
标签: java arrays split type-conversion