键盘上输入一个十位数,把每位上的数字相加,求和。应该怎么写

键盘上输入一个十位数,把每位上的数字相加,求和。应该怎么写

1
键盘上输入一个十位数,把每位上的数字相加,求和。应该怎么写


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕阿满 2018-11-28 14:16:30

同学你好,输入十位数字完全可以参考之前的代码来写。如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
            System.out.println("请输入十位数字");
            //接受从键盘输入的两位数
            long n = sc.nextLong();
            //求十亿位数上的数字
            int one = (int) (n/1000000000);
            //求亿位数上的数字
            int two = (int) (n/100000000%10);
            //求千万位上的数字
            int three = (int) (n/10000000%10);
            //求百万位上的数字
            int four = (int) (n/1000000%10);
            //求十万位上的数字
            int five = (int) (n/100000%10);
            //求万位上的数字
            int six = (int) (n/10000%10);
            //求千位上的数字
            int seven = (int) (n/1000%10);
            //求百位上的数字
            int eight = (int) (n/100%10);
            //求十位上的数字
            int night = (int) (n/10%10);
            //求个位上的数
            int ten =(int) (n%10);
            //求和
            int sum = one+two+three+four+five+six+seven+eight+night+ten;
            //输出
            System.out.println("各位和十位上的数字之和为:"+sum);
     }

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快!

好帮手慕阿满 2018-11-21 13:56:04

同学你好,可以先输入一个十位数,然后求每个位置上的数,最后相加。可参考如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个两位数");
        //接受从键盘输入的两位数
        int n = sc.nextInt();
        //求十位数上的数字
        int shi = n/10;
        //求个位数上的数字
        int ge = n%10;
        //求和
        int sum = shi+ge;
        //输出
        System.out.println("各位和十位上的数字之和为:"+sum);
    }

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快!

  • 是十位数,例如3568593248,十位数,每一位相加求和。(而且是随机打出来的十位数。)
    2018-11-28 13:27:32
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码