import java.util.Scanner; |
public class Fenli{ |
public static void main(String[] args){ |
System.out.println( "输入一个数" ); |
Scanner sc= new Scanner(System.in); |
int n=sc.nextInt(); |
int a=weishu(n); |
System.out.println(n+ "是" +a+ "位数" ); |
shu(n,a); |
} |
public static int weishu( int x){ //确定有几位数 |
int a= 10 ,n= 1 ; //n记数字的位数,a记除数 |
double b=x/a; |
if (b< 1 ) |
return n; //如果除以10小于1则是个位数,只有一位 |
else |
{ |
while (b> 1 ) //如果大于1,则a*10,n+1,循环再次判断,直到商小于1,返回位数 |
{ |
a=a* 10 ; |
n++; |
b=x/a; |
} |
return n; |
} |
} |
public static void shu( int x, int y){ //分离每位上的数 |
int a= 1 ,sum= 0 ; |
int j= 0 ; |
int c[]= new int [y]; //将分离的数存入数组中 |
for ( int i= 1 ;i<y;i++) |
{ |
a=a* 10 ; //先分离最高位 |
} |
while (a> 0 ) |
{ |
int b=x/a; |
c[j]=b; //商就是最高位上的数,存入数组 |
j++; |
x=x%a; //取余作为被除数,再循环 |
a=a/ 10 ; |
} |
for ( int i= 0 ;i<y;i++) //输出数组中的数 |
System.out.print(c[i]+ " " ); |
System.out.println( " " ); |
for ( int i= 0 ;i<y;i++) //求和 |
sum=sum+c[i]; |
System.out.print( "和是" +sum); |
} |
} |