求Sn=2+22+222+2222+22222+22...2222(最后一项n个2)java
时间: 2023-12-21 21:32:09 浏览: 105
用递归求1+2+3+...+n的程序
以下是使用Java编写的求Sn的程序:
```java
import java.util.Scanner;
public class SnCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入a和n的值:");
int a = scanner.nextInt();
int n = scanner.nextInt();
int m = 0;
int s = 0;
for (int i = 1; i <= n; i++) {
m = m + a;
s = s + m;
m = 10 * m;
}
System.out.printf("a+aa+aaa+...=%d\n", s);
}
}
```
阅读全文