[英]How can I stack up the number patterns with while loop in java?
Dear Professionals! 亲爱的专业人士! I'm a super beginner of programming java.
我是编程java的超级初学者。 I'm just learning a basic stuff in school.
我只是在学校里学习基本的东西。 While I'm doing my homework, I'm stuck in one problem.
在我做作业的时候,我遇到了一个问题。
The question is Using nested loops to make this stack-up number pattern: 问题是使用嵌套循环来构建这个堆叠数字模式:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
I can only use while loop (because we haven't learned for or do loops yet), and the outer loop body should execute 10 times. 我只能使用while循环(因为我们还没有学习或执行循环),外循环体应该执行10次。 I can use print and println for making this pattern.
我可以使用print和println制作这个图案。
I tried many different methods with while loop, but I can't figure it out. 我用while循环尝试了很多不同的方法,但我无法弄明白。
Please, please give me some hint. 拜托,请给我一些提示。
This is the code that I'm working on it so far: 到目前为止,这是我正在处理的代码:
class C4h8
{
public static void main(String[] args)
{
int i, j;
i = 1;
while(i <= 10)
{
j = 1;
while (j <= 10)
{
System.out.print(j);
j++;
}
System.out.println();
i++;
}
}
}
but it only displays: 但它只显示:
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
My question may look like a silly one, but I'm really struggling with it because like I mentioned, I'm a super beginner.. Please help me, so that I can learn and move on! 我的问题可能看起来像一个愚蠢的问题,但我真的很挣扎,因为就像我提到的,我是一个超级初学者..请帮助我,这样我就可以学习并继续前进!
Thank you so much! 非常感谢!
Use the following: You need to limit the variable j
by variable i
to achieve your output 使用以下内容:您需要通过变量
i
限制变量j
以实现输出
class C4h8
{
public static void main(String[] args)
{
int i, j;
i = 1;
while(i <= 10)
{
j = 1;
while (j <= i) // limit the variable j by i
{
System.out.print(j+" ");
j++;
}
System.out.println();
i++;
}
}
}
In less code with while loop 使用while循环的代码较少
int i = 0;
int limit = 10;
while(++i <= limit){
int j = 0;
while(++j <= i)
System.out.print(j+" ");
System.out.println();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.