Java中for循环内的图案绘制

huangapple go评论74阅读模式
英文:

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 &lt;= 5; i++){
        System.out.println(&quot;*&quot; + &quot;\n&quot;);
    }
}

}


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 &lt; 5; i++) {
    System.out.println(&quot;*&quot;.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&lt;5 and after every iteration, do i++"

for(int i = 0; i&lt;5, i++){
   System.out.println(&quot;*&quot; \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 &quot;*&quot; you create a String before the loop and you add one new * to it every iteration:

String line =&quot;&quot;;             // we start with an empty string
for(int i=0; i&lt;5; i++){      
   line= line+&quot;*&quot;;           // 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&lt;n; i++) {           
            for(j=0; j&lt;=i; j++) {       
                System.out.print(&quot;*&quot;);
            }
            System.out.println();
        }
    }     	
}

huangapple
  • 本文由 发表于 2020年5月29日 16:03:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/62081350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定