求s=a+aa+aaa+aaaa+aa...a的值

题目:求s=a+aa+aaa+aaaa+aa...a的值,
其中a是一个数字。
例如2+22+222+2222+22222(此时共有5个数相加),
几个数相加有键盘控制。

import java.util.*;
public class Test{
    public int cal(int a,int num)
    {
        int result=0;
        int i=0;
        int b=0;
        while(i<num)
        {
            b=b+a;
            result = result+b;
            b=b*10;
            i++;
        }
        return result;
    }
    public static void main(String args[]){
        Scanner scan = new Scanner(System.in);
        System.out.println("please input a integer you want to add");
        int a = scan.nextInt();
        System.out.println("input nums: ");
        int num = scan.nextInt();
        System.out.println("result is: "+new Test().cal(a,num));
    }
}

 

posted @ 2013-03-16 14:38  LaoQuans  阅读(276)  评论(0编辑  收藏  举报