输入数字a和b,计算sum=a+aa+aaa+aaaa+...

 
 
package com.smallprogram;

import java.util.Scanner;

/**
 * @date:2018-4-19
 * @author x_san
 * @describe: 输入数字a和b(只针对a为一位数字,b为的次数),计算sum=a+aa+aaa+aaaa+...
 * @describe: 例如:a=2,b=5,sum=2+22+222+2222+22222
 */
public class AN
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		int sum = 0, temp = 0, a=0, b=0;	
		System.out.println("请输入a:");
		a = input.nextInt();
		System.out.println("请输入b:");
		b = input.nextInt();
		StringBuffer buffer = new StringBuffer();
		for(int i=0; i < b; i++)
		{
			temp += a * Math.pow(10, i);
			sum += temp;
			buffer.append(temp + "+");
		}			
		String s = buffer.substring(0, buffer.length()-1);
		System.out.println("sum=" + s + "=" + sum);
	}	
	
}


猜你喜欢

转载自blog.csdn.net/x_san3/article/details/80010996