2. Write a program to find the sum of the first n items of x+xx+xxx+…… (for example: 8+88+888+……). The values of x and n are input by the user from the keyboard.

2. Write a program to find the sum of the first n items of x+xx+xxx+…… (for example: 8+88+888+……). The values ​​of x and n are input by the user from the keyboard.

package com.temp;

import java.util.Scanner;

/**
 * @Author lanxiaofang
 * @email [email protected]
 * @date 2020/09/14 08:15
 */
public class SumOfX {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入您想要求和的数:");
        int x = scanner.nextInt();
        System.out.println("请问求前几项的和?");
        int n = scanner.nextInt();
        int n1=x, sum = x;
        for(int i = 1; i < n; i++){
            n1 = n1*10+x;
            sum = sum+n1;
        }
        System.out.println("最后的和是: "+sum);
    }

}

 

 

Guess you like

Origin blog.csdn.net/c_lanxiaofang/article/details/108571831
Recommended