【发布时间】:2020-12-03 10:52:03
【问题描述】:
我的任务是为 Java 解决循环问题,但我目前在如何显示数字的阶乘方面遇到问题。例如,1x2x3x4x5 = 120。
我快到了,但我似乎无法弄清楚如何,或者是否有任何可能的方法来显示数字的阶乘,因为在 5 的末尾总是有一个额外的“x”。
这是我的代码:
import java.util.*;
public class trylangpo2 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int fctr;
System.out.println ("number");
fctr = input.nextInt();
for (int i = 1; i <=fctr; i++){
System.out.print(i);
int j;
for (j =1; j <=1 ; j++){
System.out.print("*");
}
}
}
}
示例输出:
1x2x3x4x5x
【问题讨论】:
-
是否存在因子流行病? stackoverflow.com/questions/65089542/…
-
没有解决您的问题,只是指出您的循环
for (j =1; j <=1 ; j++)可以删除。它只循环一次,只需写System.out.print("*");。不需要循环
标签: java