我想在同一个 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/