Define an integer variable and assign any five positive integers as the initial value, and output the sum of the digits

# Define an integer variable and assign any five positive integers as the initial value, and output the sum of the digits (for example: 12345 The sum of the digits is: 1+2+3+4+5. That is, 15)



public class Demo05 {
public static void main(String[] args) {
	/*随机生成五位随机数 因为生成的随机数是double类型,需要进行强制类型转化*/
	int number=(int)(Math.random()*100000);
	int  ge =number%10;//个位
	int shi = number/10%10;//十位
	int bai =number/100%10;
	int qian =number/1000%10;//千位
	int wan =number/10000%10;//万位
	System.out.println("随机数为:"+number);
	System.out.println("结果为:"+(wan+qian+bai+shi+ge));
}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324120583&siteId=291194637