源代码:
Test类:
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
System.out.println("请输入数字:");
Scanner input = new Scanner(System.in);
int k=input.nextInt();
math m = new math();
System.out.print(m.judge(k)+"位数字为:");
m.qushu(k);
}
}
math类:
import java.lang.Math;
public class math
{
private int n;
private int number[];
public int judge(int m)
{
int k=0;n=1;int q;
do
{
q=1;
for(int i=0;i<n;i++)
{ q=q*10;}
k=m/q;n++;
}
while(k!=0);
return n-1;
}
public void qushu(int m)
{
int q;int h=n-1;
int number[]=new int[n-1];
for(int i=0;i<n-1;i++)
{
q=1;
for(int a=1;a<h;a++)
{ q=q*10;}
number[i]=m/q;m=m-number[i]*q;h=h-1;
}
for(int i=0;i<n-1;i++)
{
System.out.print(number[i]+" ");
}
int result;
result=0;
for(int i=0;i<n-1;i++)
{
result=result+number[i];
}
System.out.println("位数字之和为:"+result);
}
}
截图: