package studying; import java.util.Scanner; //题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。 //例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。 public class Chengxu8 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int sum = 0; int temp = 0; System.out.println("the question is:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。"); System.out.println("Which number of loops to use:(a)"); int a = input.nextInt(); System.out.println("Loop several times:"); int b = input.nextInt(); for(int i = 0; i < b; i++) { temp = temp * 10 + a; System.out.println("The "+ (i+1) +" number is:" + temp); sum += temp; } System.out.println("The results of the calculation are:"+sum); } }
结果展示: