Java第二周:2.6**

来源:互联网 发布:模拟经营类游戏 知乎 编辑:程序博客网 时间:2024/05/22 07:57

问题及代码:

/* * 烟台大学计控学院  李楠  * 时间:2015\9\8 * 要求:输入一个0~1000之间的整数,输出各个位数相加的和。 * 输入:999 * 输出:The sum of the digits is 27 */import java.util.Scanner;public class Hello{/** * @param args */public static void main(String[] args){// TODO Auto-generated method stubint a;Scanner in=new Scanner(System.in);System.out.print("Enter a number between 0 and 1000:");a=in.nextInt();int b;b=a%10+(a/10)%10+a/100;System.out.println("The sum of the digits is "+b);}}


运行结果:

0 0