Java基礎編程題:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數字。

import java.util.Scanner;
//求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數字。
public class Main {
    public static void main(String[] args) {
        double sum=0;
        double temp=0;
        System.out.println("The equation is S=a+aa+aaa+aa..(na).a");
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the a :");
        int a = scanner.nextInt();
        System.out.print("Enter the n :");
        int n = scanner.nextInt();
        System.out.printf("a is %d , n is %d .",a,n);
        
        for(int i=0;i<n;i++) {
        	temp=temp+a*Math.pow(10, i);
        	sum=sum+temp;
        }
        
        System.out.println("S = "+sum);
    }
 
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章