[英]How can I repeat text in a manipulative way, and repeat this action until my target has been reached?
How do I count up from 1 to a given number in triangular fashion?如何以三角形方式从 1 数到给定数字?
Attempt试图
while (numbers <= number) {
System.out.println(numbers);
numbers++;
}
Target Output目标 Output
1
1 2
1 2 3
I recommend you read about looping in Java, this is a base to start from.我建议您阅读 Java 中的循环,这是开始的基础。
int num = 10;
for(int i = 1; i < num; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.print("\n");
}
This should provide the triangular structure you are looking for.这应该提供您正在寻找的三角形结构。 Definitely do take a look at the link above.
一定要看看上面的链接。 Since you are new to Java, you should take a walk through this code before simply copying it.
由于您是 Java 的新手,因此您应该先浏览一下此代码,然后再简单地复制它。 Really try to see how it works and more importantly, why it works.
真正尝试看看它是如何工作的,更重要的是,它为什么会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.