簡體   English   中英

JOptionPane 上的 Java 因子

[英]Java Factorial On JOptionPane

我想在同一個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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM