JAVA-------------八、計算a+aa+aaa+.....

 1 import java.util.*;  2 public class Text_8 {  3 
 4     public static void main(String[] args) {  5         /*
 6  * 求s=a+aa+aaa+aaaa+....的值,其中a是一個數字。  7  * 例如:2+22+222+2222+22222 此時n=5,a是基數  8          */
 9         Scanner scan=new Scanner(System.in); 10         System.out.println("輸入你要相加的個數:"); 11         int n=scan.nextInt(); 12         System.out.println("輸入基數:"); 13         int a=scan.nextInt();//基數m
14         int sum=a,t=a; 15         for(int i=1;i<n;i++) { 16             t=t*10+a;//aa=a*10+a;aaa=aa*10+a;....
17             sum=sum+t;//求和
18             
19  } 20  System.out.println(sum); 21 
22  } 23 
24 }
相關文章
相關標籤/搜索