Question: calculate the value of s = a + aa + aaa + aaaa + aa... a, where a is a number ., Aaaaaa...
I. First Writing Method
Package com. pb. demo1; import java. util. answer;/*** question: s = a + aa + aaa + aaaa + aa... the value of a, where a is a number. For example, 2 + 22 + 222 + 2222 + 22222 (a total of 5 numbers are added at this time), * the number of a and the number of accumulated digits is received from the keyboard. Program Analysis: * 1. Receive a number input from the keyboard (numbers can only be between 1 and 9) * 2. Receive a number to indicate the number of accumulated digits * 3. for example, if you enter 4 (the number of accumulated digits), the value of a on the keyboard is 5: s = 5 + 55 + 555 + 5555 4. enter a 5: s = a + aa + aaa + aaaa + aaaaa * First Writing Method */public class Demo2 {public static void main (String [] args) {// declare a Scanner variable named Scanner input = new Scanner (System. in); System. out. println ("Enter the numbers involved in the calculation 1-9:"); int n = input. nextInt (); System. out. println ("Enter the number of operations:"); int num = input. nextInt (); // defines the intermediate variable of each loop to calculate the number of double nums = 0.0 each time; // The total number of last listen double sum = 0.0; for (int I = 0; I <num; I ++) {// Add N to the number of each time to the new n value nums + = n; // sum + = nums; // The value of n is added with n = n * 10 each time; System. out. println ("the number of each operation is:" + nums);} System. out. println ("the final sum is:" + sum );}}
Ii. method 2
Package com. pb. demo1; import java. util. answer;/*** question: s = a + aa + aaa + aaaa + aa... the value of a, where a is a number. For example, 2 + 22 + 222 + 2222 + 22222 (a total of 5 numbers are added at this time), * the number of a and the number of accumulated digits is received from the keyboard. Program Analysis: * 1. Receive a number input from the keyboard (numbers can only be between 1 and 9) * 2. Receive a number to indicate the number of accumulated digits * 3. for example, if you enter 4 (the number of accumulated digits), the value of a on the keyboard is 5: s = 5 + 55 + 555 + 5555 4. enter a 5: s = a + aa + aaa + aaaa + aaaaa * method 2 */public class Demo3 {public static void main (String [] args) {// declare a Scanner variable named Scanner input = new Scanner (System. in); System. out. println ("Enter the numbers involved in the calculation 1-9:"); int n = input. nextInt (); System. out. println ("Enter the number of operations:"); int num = input. nextInt (); // defines the intermediate variable of each loop to calculate the number of double nums = 0.0 each time; // The total number of last listen double sum = 0.0; for (int I = 0; I <num; I ++) {// calculate nums + = Math using the mathematical power function. pow (10, I) * n; sum + = nums; System. out. println ("the number of each operation is:" + nums);} System. out. println ("the final sum is:" + sum );}}