首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JOptionPane上的Java Factorial

JOptionPane上的Java Factorial
EN

Stack Overflow用户
提问于 2020-01-30 21:28:08
回答 1查看 76关注 0票数 0

我想在同一个JOptionPane对话框中显示阶乘结果和工作(计算),例如1x2x3x4x5=120和花费的小时数,但还没有找到解决方案。任何帮助都将受到高度的感谢。:)

代码语言:javascript
复制
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);   
}
EN

回答 1

Stack Overflow用户

发布于 2020-01-30 21:39:03

你可以这样做

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59987149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档