[英]How to print this number pattern using loops in JAVA?
How do I print this pattern modifying my original code below:如何打印此模式,修改我的原始代码如下:
I am a beginner in java and not able to create the pattern listed below.我是 Java 初学者,无法创建下面列出的模式。
Pattern:图案:
1
1 ,1
1 ,2 ,1
1 ,3 ,3 ,1
1 , 4 , 6 , 4 , 1
1 , 5 , 1 0 , 1 0 , 5 , 1
My Code:我的代码:
public class Q2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count = 5;
for (int i = 1; i <= count; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
}
}
My Output:我的输出:
1
22
333
4444
55555
I wish I could comment but I don't have enough reputation.我希望我可以发表评论,但我没有足够的声誉。
You need to add something to Yimin's answer to calculate the factorial, for example.例如,您需要在 Yimin 的答案中添加一些内容来计算阶乘。
static int factorial(int n){
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
What you want is i choose j
for each term.你想要的是
i choose j
为每个术语i choose j
。 This is by formula:这是通过公式:
i!/(j!*(i-j)!)
public static void main(String[] args) {
int count = 5;
for (int i = 0; i <= count; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(factorial(i) / (factorial(j) * factorial(i - j)));
System.out.print(' ');
}
System.out.println();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.