7-4 jmu-Java-01入门-取数字浮点数 (10分)

本题目要求读入若干以回车结束的字符串表示的整数或者浮点数,然后将每个数中的所有数字全部加总求和。

输入格式:

每行一个整数或者浮点数。保证在浮点数范围内。

输出格式:

整数或者浮点数中的数字之和。题目保证和在整型范围内。

输入样例:

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);
		}
	}
}
原创文章 14 获赞 12 访问量 1202

猜你喜欢

转载自blog.csdn.net/weixin_45713984/article/details/105880628