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

代码:

package com.liron.p1;

import java.io.IOException;
import java.util.Scanner;

/**
 * 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。
 * 例如2+22+222+2222+22222(此时共有5个数相加),几个数相
 * 加有键盘控制。
 */
public class Topic18 {
    public static void main(String[] args) throws IOException  
    {  
        Scanner sc = new Scanner(System.in);
        System.out.println("用哪个数循环?:");
        int _temp = sc.nextInt();  
        System.out.println("循环相加多少次?:");
        int temp = sc.nextInt();
   
        int newNumber = 0; // 每次生成的新数  
        int total = 0; // 计算结果  
        for (int i = 0; i < temp; i++)  
        {  
            newNumber = newNumber * 10 + _temp;  
            System.out.println(newNumber);  
            total = total + newNumber;  
        }  
        System.out.println("计算结果:" + total);  
    }
}

 

结果:

 

posted @ 2017-04-17 20:10  小皮卡  阅读(3729)  评论(0编辑  收藏  举报