急.....請問JAVA2作業問題!

題目:請撰寫一個類別Calculator, 利用 static 方法提供計算次方 、 階乘等方法.

希望各位大大們能夠把程式post出來.

先感謝回答問題的大大.感恩不盡!

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    public class Calculator

    {

    public static double getProd(double num, int times)

    {//計算次方

    double sum = 1;

    for (int i = 0 ; i < times ; i ++)

    {

    sum = sum * num;

    }

    return sum;

    }

    public static double getStepProd(double num)

    {//階乘

    double sum = 1;

    for (int i = 1 ; i <= num ; i ++)

    {

    sum = sum * i;

    }

    return sum;

    }

    public static void main(String[] args)

    {

    System.out.println(Calculator.getProd(4, 4));

    System.out.println(Calculator.getStepProd(3));

    }

    }

    執行結果:

    256.0

    6.0

    Source(s): 自己
Still have questions? Get your answers by asking now.