s Java o = a + aa + aaa + aaaa + aa... The sum of five a Numbers

  • 2020-06-12 09:02:54
  • OfStack

s=a+aa+aaa+aaaa+aa... The value of a, where a is a number. For example, 2+22+222+2222+22222(at this time a total of 5 Numbers added), several Numbers added with keyboard control.

Program analysis: The key is to calculate the value of each item.

Program design:


import java.io.*;
public class Sumloop {
 public static void main(String[] args) throws IOException
 {
   int s=0;
   String output="";
   BufferedReader stadin = new BufferedReader(new InputStreamReader(System.in));
   System.out.println(" Please enter the a The value of the ");
   String input =stadin.readLine();
   for(int i =1;i<=Integer.parseInt(input);i++)
   {
     output+=input;
     int a=Integer.parseInt(output);
     s+=a;
   }
   System.out.println(s);
 }
}

Another solution:


import java.io.*;
public class Sumloop {
 public static void main(String[] args) throws IOException
 {
   int s=0;
   int n;
   int t=0;
   BufferedReader stadin = new BufferedReader(new InputStreamReader(System.in));
   String input = stadin.readLine();
   n=Integer.parseInt(input);
   for(int i=1;i<=n;i++){
    t=t*10+n;
    s=s+t;
    System.out.println(t);
   }
   System.out.println(s);
   }
}

Java =a+aa+aaa+aaaa+aa... a 5 Numbers add the value of the implementation code, the need for friends can refer to 1.


Related articles: