Java 学习输入1234 求和

输入任何整数都能求和

 1 import java.util.Scanner;
 2 
 3 /**
 4  * @author 作者anil E-mail:888@yirose.com
 5  * @date 创建时间:2017年3月23日 上午9:17:19
 6  * @version 1.0
 7  * @parameter
 8  * @since
 9  * @return
10  * @tags
11  *
12  */
13 
14 public class totals {
15     public static void main(String[] args) {
16         
17 //        int totals = 0;
18 //        for (int i = 1; i <= 4; i=1) {
19 //            System.out.print("请输入整数:");
20 //            Scanner in = new Scanner(System.in);
21 //            int op = in.nextInt();            
22 //            totals += op;
23 //            System.out.println(totals);
24 //
25 //        }        
26         
27         for (int i = 1; i <= 4; i=1) {
28             int totals = 0;
29             System.out.print("请输入整数:");
30             Scanner in = new Scanner(System.in);
31             int op = in.nextInt();    
32             String s = String.valueOf(op);
33             String[] a = s.split("");
34             for (int j = 0; j < a.length; j++) {    
35 //                int a1 = Integer.parseInt(a[j]);
36                 totals += Integer.parseInt(a[j]);
37 //                System.out.println(a1);                
38             }    
39             System.out.println(totals);            
40 
41         }
42     }
43 }
原文地址:https://www.cnblogs.com/pxuan/p/6603730.html