输入一个正整数,求它各位数的数字之和

class Test{
  public static void main(String[] args){
    int iSum = 0;
    Scanner scan = new Scanner(System.in);
    System.out.print("请输入一个正整数:");
    int iNum = scan.nextInt();
    int iC = iNum;
    while(iNum % 10 == 0)
      iNum = iNum / 10;
    while(iNum % 10 > 0){
      int iTemp = iNum % 10;
      iSum += iTemp;
      iNum = iNum / 10;
      if(iNum == 0)
        break;
      while(iNum % 10 == 0)
        iNum = iNum / 10;
    }
    System.out.println(iC + "的各位数字之和为:" + iSum);
  }
}

 

posted @ 2015-08-01 11:55  飞牛冲天  阅读(2494)  评论(0编辑  收藏  举报