Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Code demo:

Where a is a number from 1 to 9, and the number of terms must also be specified

package csdncom.tt;

import java.util.Scanner;

/** * Created by Java java.java.java on 2021/10/30. */
public class Sum {
	public static void main(String[] args) {
		int a; // Define the input a
		int howMany; // Define how many numbers the last item has
		Scanner scanner = new Scanner(System.in);
		System.out.println("Please enter an A value from 1 to 9.");

		a = scanner.nextInt();
		System.out.println("How many terms do YOU want to add?");
		howMany = scanner.nextInt();
		int sum = 0;
		int a1 = a; // Hold the initial value of a
		for (int i = 1; i <= howMany; i++) {
			sum += a;
			a = 10 * a + a1; // This represents the next term of A
			// Each time the next term of a is equal to the previous term *10, plus the a that was entered; Notice that the a has changed.
		}
		System.out.println("sum="+ sum); }}Copy the code

Print: