java 2.6** Summing the digits in an integer

例如 : 999

process: sum=9+9+9=27;

例如 : 932

process: sum=9+3+2=14;

 

import java.util.Scanner;


public class Main {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		int number;
		System.out.println("Enter a number between 0 and 1000: ");
		number=input.nextInt();
		
		int sum=0;
		while(number!=0)
		{
			sum=sum+number%10;
			number=number/10;
		}
		 
		System.out.println("The sum of the digits is "+sum);
	}
}


 

你可能感兴趣的:(java 2.6** Summing the digits in an integer)