英文:
Java patten draw within for loop
问题
我在编程世界和 Java 世界都是新手。
我正在尝试学习使用 for 循环并用它来解决图案问题。
我想使用 for 循环绘制以下图案:
*
**
***
****
*****
我正在使用以下代码来实现:
```java
public class PatterWithForLoop {
public static void main(String args[]){
for(int i = 0; i <= 5; i++){
System.out.println("*" + "\n");
}
}
}
但是,我只得到了在 15 行中都是 *,而不是图案。
请问有谁能帮忙解决这个问题吗?
提前谢谢。
<details>
<summary>英文:</summary>
I am very new in the programming world and in Javaland too.
I am trying to learn for loop and solving pattern with it.
I want to draw this pattern using for loop :
*
**
***
****
*****
I am using this code to achieve this:
public class PatterWithForLoop {
public static void main(String args[]){
for(int i = 0; i <= 5; i++){
System.out.println("*" + "\n");
}
}
}
But, I am only getting * in 15 lines, not in the pattern.
Please, anyone can help to solve this issue here?
Thanks in advance.
</details>
# 答案1
**得分**: 4
如果您使用的是JDK 11,您可以编写以下代码:
```java
for (int i = 0; i < 5; i++) {
System.out.println("*".repeat(i + 1));
}
通过String.repeat(),您可以用更少的代码实现相同的效果。
英文:
If you are using JDK 11, you can write the following code:
for(int i = 0; i < 5; i++) {
System.out.println("*".repeat(i+1));
}
With String.repeat(), you can do the same thing with less code.
答案2
得分: 2
Your for-loop seems strange.
The loop should be read like this:
"For int
i
starting at 0
, loop as long as i<5
and after every iteration, do i++
"
for(int i = 0; i<5; i++){
System.out.println("*" \n);
}
This will print 5 rows:
*
*
*
*
*
Now in every row, you want one more *
, and you might want to remove the \n
which is already included in println
. So instead of printing "*"
, you create a String
before the loop and you add one new *
to it every iteration:
String line = ""; // we start with an empty string
for(int i=0; i<5; i++){
line = line + "*"; // before printing we add a new *
System.out.println(line); // then we print it
}
It should (haven't tested it) result in this:
*
**
***
****
*****
英文:
Your for-loop seems strange.
The loop should be read like this:
"For int
i
starting at 0
, loop as long as i<5
and after every iteration, do i++
"
for(int i = 0; i<5, i++){
System.out.println("*" \n);
}
This will print 5 rows:
*
*
*
*
*
now in every row you want one more *
and you might want to remove the \n
which is already included in println
So instead of printing "*"
you create a String
before the loop and you add one new *
to it every iteration:
String line =""; // we start with an empty string
for(int i=0; i<5; i++){
line= line+"*"; // before printing we add a new *
System.out.println(line); // then we print it
}
It should (haven't tested it) result in this:
*
**
***
****
*****
答案3
得分: 1
以下是您要的翻译内容:
尝试这段代码:
public class Main {
public static void main(String[] args) {
int i, j, n = 5;
for(i = 0; i < n; i++) {
for(j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
英文:
Try this code :
public class Main {
public static void main(String[] args) {
int i, j, n =5;
for(i=0; i<n; i++) {
for(j=0; j<=i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论