JOptionPane 上的 Java 阶乘

标签 java joptionpane factorial

我想在同一个 JOptionPane 对话框中显示阶乘结果和工作(计算),例如 1x2x3x4x5=120 并花了几个小时但尚未找到解决方案。任何帮助将不胜感激。 :)

private fun uploadWithTransferUtility(remote: String, local: File) {
   String number = JOptionPane.showInputDialog("Please enter the number below ");

   int n = Integer.parseInt(number);
   long fact = 1;
    int i = 1;
    if (n<=0){
    JOptionPane.showMessageDialog(null," Please enter a possitive number");

    }
    else{

     while(i<=n)
    {


    if (i==1){
            fact = fact * i;
            System.out.print(i);
            i++;
        }
        else{
            fact = fact * i;
            System.out.print("*"+i);
            i++;
    }


    JOptionPane.showMessageDialog(null,"="+fact);   
}

最佳答案

你可以这样做

int n = Integer.parseInt(number);
long fact = 1;
int i = 1;
if (n <= 0) {
    JOptionPane.showMessageDialog(null, " Please enter a possitive number");
} else {
    StringJoiner stringJoiner = new StringJoiner("x"); //You can use "*" if you want
    for (i = 1; i <= n; i++) {
        fact = fact * i;
        stringJoiner.add(i + "");
    }

    JOptionPane.showMessageDialog(null, stringJoiner.toString() + "=" + fact);
}

关于JOptionPane 上的 Java 阶乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59987149/

相关文章:

C++函数计算阶乘返回负值

matlab - 多项式系数的高效 Matlab 实现

java - 从 Jquery 调用 Bean 方法

java - 找不到合适的方法来解析(java.io.InputStream)

java - 如何限制Joptionpane消息对话框条件提示一次?

java - JOptionPane 边框的颜色

java - android okohttp 将数据插入 php 不起作用

java - Oracle T4CPreparedStatement 内存泄漏?

java - JOptionPane showOptionDialog

java - 计算阶乘的递归函数在 20 之后失败