如何通过调用我的阶乘方法编写我的Main类

时间:2015-11-12 20:22:01

标签: java factorial

这是我的计算方法,用于查找用户编号的阶乘。

public class Factorial {

   public static double factorial(double x) {
   double fact = 1;

   for (int i=2 ; i <= x ; i++) {
     fact = fact * i;
   }

   return fact;
}

主要是我认为是我的问题,我只是想弄清楚如何正确调用我的void方法,以便它能正常工作

public static void main(String[] args) {

    String inputStr = JOptionPane.showInputDialog("Enter a number: ");
    int x = Integer.parseInt(inputStr);
    int fact = Integer.parseInt(inputStr);
    JOptionPane.showMessageDialog(null, "The factorial of the number is: "+ fact);
}

1 个答案:

答案 0 :(得分:1)

package com.company;

public class Main {

    public static void main(String[] args) {

        final double factorial = Factorial.factorial(2.3);
    }
}

你可以这样做。

相关问题