求Sn=2+22+222+…+22…222(有n个2)的值。n由键盘输入。java
时间: 2024-06-14 15:04:18 浏览: 14
以下是使用Java编写的求Sn=2+22+222+…+22…222(有n个2)的值的代码示例:
```java
import java.util.Scanner;
public class SnValue {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入n的值:");
int n = scanner.nextInt();
int a = 2;
int sum = 0;
int temp = 0;
for (int i = 1; i <= n; i++) {
temp = temp * 10 + a;
sum += temp;
}
System.out.println("Sn的值为:" + sum);
}
}
```
相关问题
java.求Sn=2+22+222+…+22…222(有n个2)的值。例如:2+22+222+2222+22222(n=5),n由键盘输入。
以下是使用Java求Sn=2+22+222+…+22…222(有n个2)的值的代码示例:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int temp = 0;
for(int j=1;j<=n;j++) {
for(int i=1;i<=j;i++) {
temp += 2 * (int)Math.pow(10, i-1);
}
}
System.out.print(temp);
}
}
```
求Sn=2+22+222+2222+22222+22...2222(最后一项n个2)java
以下是使用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);
}
}
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)