用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

一只来自南方的鬼    -  云代码空间

——

输入一个0~1000的数,求各位数之和

2017-03-19|852阅||

摘要:import java.util.Scanner; public class DigitSum { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int inputInt

import java.util.Scanner;


public class DigitSum {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int inputInt, result = 0;
do {
System.out.println("请输入一个0~1000的整数");
inputInt = sc.nextInt();// 接收一个整型数据


if (inputInt >= 0 && inputInt <= 1000) {
break;
} else {
System.out.println("输入数据不满足要求,请重新输入!\n");
}
} while (true);
int tmp = inputInt;
while (tmp > 0)//循环取得最后一位数字并相加
{
result += tmp % 10;//每次循环取得最后一位数字
tmp /= 10;//获取去掉最后一位数字后的值
}
System.out.println(inputInt + " 的各位数字之和为:" + result);
}
}
顶 0踩 0收藏
文章评论
    发表评论

    个人资料

    • 昵称: 一只来自南方的鬼
    • 等级: 中级程序员
    • 积分: 150
    • 代码: 0 个
    • 文章: 2 篇
    • 随想: 0 条
    • 访问: 0 次
    • 关注

    人气代码

      标签

      站长推荐