简体   繁体   English

JOptionPane 上的 Java 因子

[英]Java Factorial On JOptionPane

I want to show factorial result and work(calculation) in same JOptionPane dialog box, as example 1x2x3x4x5=120 and spent hours but haven't found a solution.我想在同一个JOptionPane对话框中显示阶乘结果和工作(计算),例如1x2x3x4x5=120并且花了几个小时但没有找到解决方案。 Any help will be highly appreciated.任何帮助将不胜感激。 :) :)

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);   
}

You can do it like this你可以这样做

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM