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

[亡魂溺海] 提交于 2019-12-02 22:47:28

问题


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:

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

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. )


回答1:


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 really trying 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");
    }
}



回答2:


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.

Use System.out.print("*") to print without adding a new line, at the end of the second loop do a 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




回答3:


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

  1. The for loops should always start from 0, unless you have a specific reason to start from 1. Its a good habit to practice starting from 0 for everything, as it'll help you when it comes to using java arrays.
  2. The 2 for loops inside each other... The outside loop is just controlling how many lines there are in the triangle (8 in 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 for loop at the bottom is writing out stars to appear like the trunk of a tree.

Hope this helps your understanding.




回答4:


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");
}
}
}



回答5:


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();
}
}



回答6:


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();
    }

    }
 }



回答7:


          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);


        }           
    }
}



回答8:


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();



回答9:


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();
                     }
}


来源:https://stackoverflow.com/questions/10932962/how-can-i-print-a-triangle-with-s-using-for-loop-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!