求一个数是几位数,并求每位数相加的和
//徐召阳 20163419 信1605-3
import java.util.ArrayList;
import java.util.Scanner;
public class Zhengshu
{
public static void main(String[] args)
{
System.out.print("请输入要计算的N");
Scanner scanner=new Scanner(System.in);
int number=scanner.nextInt();
scanner.close();
Zhengshu d=new Zhengshu(number);
d.qushu();
System.out.println(d.panduan()+"位数是");
for(int i=0;i<d.list.size();i++)
System.out.println(d.list.get(i));
System.out.println(d.panduan()+"位数字之和是"+d.add());
}
public ArrayList list = new ArrayList(0);//定义一个动态数组
int number;
public Zhengshu(int number)
{
this.number = number;
}
public int panduan()//位数的判断
{ int number1 = number;
int a=1;
while(number1/10!=0)
{
a=a+1;
number1=number1/10;
}
return a;
}
public void qushu()//各个位数的显示
{
int b=number;
while(b!=0)
{
list.add(b%10);
b=b/10;
}
}
public int add()//各个位数的相加
{
int c=0;
for(int i=0;i<list.size();i++)
{
c=c+(int)(list.get(i));
}
return c;
}
}