calculate circle area


Submit solution

Points: 40 (partial)
Time limit: 1.0s
Memory limit: 256M

Authors:
Problem type
Allowed languages
Java 19, Java 8

題目說明

請設計一副程式,讓使用者輸入一整數後回傳該整數半徑的圓形面積,並將結果取至小數第二位

輸入限制 正整數

輸入

半徑

輸出

圓面積 (請使用3.14做計算)

測試資料0 輸入

3

測試資料0 輸出

28.26

System.out.printf("%.2f", ...);


Comments


  • 0
    scu09156146  commented on April 26, 2024, 2:04 p.m.

    題解

    import java.util.Scanner;
    
    public class CircleArea {
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int n = input.nextInt();
            input.close();
            // 呼叫副程式,印出回傳值(四捨五入,小數點後兩位)
            System.out.printf("%.2f", area(n));
        }
    
        public static double area(int n) {
            return 3.14 * n * n; // 圓形面積公式
        }
    
    }