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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值