求Sn=2+22+222+…+22…222(有n个2)的值。n由键盘输入。java
时间: 2024-06-14 19:04:18 浏览: 95
以下是使用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);
}
}
```
用Java求 Sn =222+222+…+22…222(有 n 个2)的值。例如:2+22+222+2222+22222( n =5), n 由键盘输入。输入n 输出和 样例输入5 样例输出24690
以下是使用Java求解Sn = 222+222+…+22…222(有n个2)的值的代码示例:
```java
import java.util.Scanner;
public class SnSum {
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 = ;
int temp = 0;
(int i = 1; i <= n; i++) {
temp = temp * 10 + a;
sum += temp;
}
System.out.println("Sn的值为:" +在Java中实现键盘输入?
2. 如何在Java中使用循环语句?
3. 如何在Java中进行数值的累加运算?
阅读全文