1 public class Demo {
 2 
 3 public static void main(String[] args) {
 4   Count(137246);
 5 }
 6 
 7 /**
 8 * 求一个整数中奇数与偶数的和
 9 * 
10 * @param a
11 */
12 public static void Count(int a) {
13   int x = 0;
14   int y = 0;
15   String string = String.valueOf(a);// 将整数转为字符串
16   char[] c = string.toCharArray();
17   for (int i = 0; i < string.length(); i++) {
18     int b = Integer.parseInt(c[i] + "");
19     if (b % 2 == 0) {
20       x += b;
21     } else {
22       y += b;
23   }
24 }
25 System.out.println(a + " 奇数的和:" + y);
26 System.out.println(a + " 偶数的和:" + x);
27 }
28 }

 

posted on 2017-08-09 12:16  微笑的喵~  阅读(285)  评论(0编辑  收藏  举报