课堂测试:输入整数,求出各位数字的和
import javax.swing.JOptionPane;
public class Add {
public static void main(String[] args) {
String str=JOptionPane.showInputDialog("输入一个数");
int n,number,power,j,sum,i;
number=Integer.parseInt(str);
power=1;
i=0;
sum=0;
j=number;
while( j!=0 )
{
power=power*10;
j=j/10;
i=i+1;
}
power=power/10;
System.out.println(i+"位数字是:");
while( power>=1 )
{
n=number/power;
System.out.println(n);
sum=sum+n;
number%=power;
power/=10;
}
System.out.println("和是"+sum);
}
}