问题及代码:
/*
* 烟台大学计控学院 李楠
* 时间: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 stub
int 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);
}
}
运行结果: