本题目要求读入若干以回车结束的字符串表示的整数或者浮点数,然后将每个数中的所有数字全部加总求和。
每行一个整数或者浮点数。保证在浮点数范围内。
整数或者浮点数中的数字之和。题目保证和在整型范围内。
123.01
234
7
9
import java.util.Scanner;
//FloatingPoint
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
String s = sc.nextLine();
char[] ch = s.toCharArray();
int sum = 0;
for (int i = 0; i < ch.length; i++) {
if (ch[i] == '-'|| ch[i] == '.') {
} else {
//将字符char类型转换成int类型
int temp = Integer.parseInt(String.valueOf(ch[i]));
sum += temp;
}
}
System.out.println(sum);
}
}
}