1 public class Test { 2 public static void main(String[] args) { 3 /* 4 * 求a+aa+aaa+aaaa的和 5 */ 6 int a = 1; 7 int n = 3; 8 int num = 0; 9 int sum = 0; 10 // 循环是输出 a aa aaa 11 //所以还要用sum相加 12 for (int j = 0; j < n; j++) { 13 num = num * 10 + a; // 1 11 111 14 System.out.print(num + " "); 15 sum += num; 16 } 17 System.out.println(); 18 System.out.println(sum); 19 } 20 21 } 22 //执行结果: 23 1 11 111 24 123