java 如何在java中使用for循环打印带有“*”的三角形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10932962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:08:02  来源:igfitidea点击:

How can i print a triangle with "*"s using for loop in java?

javafor-loopconsole-output

提问by Berk Elmas

I want to draw a triangle with stars like below using for loop, but i really don't have any idea of how to do this ? Triangle is going to be like this:

我想使用 for 循环绘制一个带有如下星星的三角形,但我真的不知道该怎么做?三角形将是这样的:

*
**
***
****
*****
******
*******
********
*********
**********

and so on. Can anybody please help me ?

等等。有人可以帮我吗?

public class Project1 {
    public static void main (String[] args){
        int c, d, e;
        for (c = 1 ; c <= 8 ; c++){
            for (d = 1 ; d <= c ; d++){
                System.out.print ("*");
            }
            System.out.println("");
        }

        for (e = 1 ; e <= 4 ; e++){
            System.out.println ("***");
        }
    } 
} 

This is what i have found from the internet, but i did not understand the reason why it uses two loops. ( i understood the one used to construct the stem. )

这是我从互联网上找到的,但我不明白它使用两个循环的原因。(我理解用于构建茎的那个。)

回答by Alex W

public static void main(String[] args)
{

    StringBuilder stars = new StringBuilder();

    for(int i = 0; i <= 10; i++)
    {
           stars.append("*");
           System.out.println(stars);
    }

}

Or alternatively using nested loops: (This is what the exercise was reallytrying to get you to do)

或者使用嵌套循环:(这是练习真正想让你做的)

public static void main(String[] args)
{
    for(int i = 0; i <= 10; i++)
    {
        for(int j=0; j<=i; j++)
        {
            System.out.print("*");
        }
        System.out.print("\n");
    }
}

回答by Joost

You will need two for-loops; one to print the line, and one to print the characters in a line. The number of the current line can be used to print a certain number of stars.

您将需要两个 for 循环;一个打印行,一个打印一行中的字符。当前行的编号可用于打印一定数量的星星。

Use System.out.print("*")to print without adding a new line, at the end of the second loop do a System.out.println()

使用System.out.print("*")不添加一个新行打印,在第二循环的结束做System.out.println()

I'll leave the implementation of the loops as an exercise, here is the syntax: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

我将循环的实现作为练习,这里是语法:http: //docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

回答by wattostudios

Just to comment on your internet-found code...

只是为了评论您在互联网上找到的代码...

  1. The forloops should always start from 0, unless you have a specific reason to start from 1. Its a good habit to practice starting from 0for everything, as it'll help you when it comes to using java arrays.
  2. The 2 forloops inside each other... The outside loop is just controlling how many lines there are in the triangle (8in this case). The inner loop is writing the number of stars for that line. This isn't the best way of achieving the result, but it would work correctly.
  3. The forloop at the bottom is writing out stars to appear like the trunk of a tree.
  1. for循环应该总是从开始0,除非你有特殊原因,从开始1。从0一切开始练习是一个好习惯,因为它会在使用 java 时为您提供帮助arrays
  2. for彼此内部的 2 个循环......外部循环只是控制三角形中有多少条线(8在这种情况下)。内循环正在写入该行的星数。这不是实现结果的最佳方式,但它会正常工作。
  3. for底部的循环写出星星,看起来像树干。

Hope this helps your understanding.

希望这有助于您的理解。

回答by Tushar Paliwal

This can give you solution of your question.

这可以为您提供问题的解决方案。

class Seven 
{
public static void main(String arg[])
{
for(int i=0;i<=8;i++)
{
for(int j=8;j>=i;j--)
{
System.out.print(" ");
}
for(int k=1;k<=(2*i+1);k++)
{
System.out.print("*");
}
System.out.println("\n");
}
}
}

回答by Mary Joy Rosellosa

import java.util.Scanner;
public class apple{
public static void main(String[] args){

int c,r;
for(c=1; c<=10; c++){
for(r=1; r<=c; r++){
System.out.print("*");
}
System.out.println();
}
}

回答by user3811027

public class StarA {

public static void main(String[] args) {

    for( int i = 1; i <= 5; i++ )
    {
        for( int j = 0; j < i; j++ )
        {
            System.out.print("*");

        }
        System.out.println();
    }

    }
 }

回答by abdul salam

          import java.util.*;

          class StarForloop
{
          public static void main(String arg[])
    {
          Scanner ob=new Scanner(System.in); //getting input
          System.out.println("Enter any NO");

          int count=ob.nextInt(); 
        String ab="*";  // initialize string variable

          for(int i=1; i<=count; i++)
        {

           ab=ab+"*"; // here you add one another string
            System.out.println(ab);


        }           
    }
}

回答by Rakesh

for(int aj =5;aj>=1;aj--){

            for (int a1 = 0; a1 < aj; a1++) {
                System.out.print(" ");
            }
                    for(int a2 = 5;a2>=aj;a2--) {
                        System.out.print("$");
                    }
                for(int a2 = 5;a2>aj;a2--) {
                    System.out.print("$");
                }
            System.out.println();

回答by Syed Salman Hassan

you just need two loops for your required goal the third loop is useless the first outer loop is for rows and the inner loop is for printing "*".The outerloop is used here for changing the rows and maintaining the number of required rows.

您只需要两个循环来实现您所需的目标,第三个循环没用,第一个外循环用于行,内循环用于打印“*”。外循环用于更改行并保持所需行数。

public static void tri()
{ 
     for(int i=0;i<8;i++)
       {
          for(int j=0;j<=i;j++)
             {
                System.out.print("*");
                }
                   System.out.println();
                     }
}