Printing *s as triangles in Java?

后端 未结 21 1860
囚心锁ツ
囚心锁ツ 2020-11-30 11:41

My assignment in my Java course is to make 3 triangles. One left aligned, one right aligned, and one centered. I have to make a menu for what type of triangle and then input

相关标签:
21条回答
  • 2020-11-30 12:23
    (a)   (b)        (c)   (d)
    * ********** ********** *
    ** ********* ********* **
    *** ******** ******** ***
    **** ******* ******* ****
    ***** ****** ****** *****
    ****** ***** ***** ******
    ******* **** **** *******
    ******** *** *** ********
    ********* ** ** *********
    ********** * * **********
    
    int line;
    int star;
    System.out.println("Triangle a");
            for( line = 1; line <= 10; line++ )
            {
                for( star = 1; star <= line; star++ )
                {
    
                    System.out.print( "*" );
                }
                System.out.println();
            }
    
     System.out.println("Triangle b");
    
              for( line = 1; line <= 10; line++ )
            {
                for( star = 1; star <= 10; star++ )
                {
    
            if(line<star)
                    System.out.print( "*" );
                    else
                      System.out.print(" ");
                }
                System.out.println();
            }
    
     System.out.println("Triangle c");
    
              for( line = 1; line <= 10; line++ )
            {
                for( star = 1; star <= 10; star++ )
                {
    
            if(line<=star)
                    System.out.print( "*" );
                    //else
                     // System.out.print(" ");
                }
                System.out.println();
            }
    
     System.out.println("Triangle d");
    
              for( line = 1; line <= 10; line++ )
            {
                for( star = 1; star <= 10; star++ )
                {
    
            if(line>10-star)
                    System.out.print( "*" );
                    else
                      System.out.print(" ");
                }
                System.out.println();
            }
    
    0 讨论(0)
  • 2020-11-30 12:25

    Question:

          *
         ***
        *****
    

    Answer:

        int i,a,j;
        i=5;
        while (i>=3)
        {
            a=1;
            while (a<=i)
            {
    
                System.out.print(" ");
                a++;
            }
            j=10;
            while (j/2>=i)
            {
                System.out.print("*");
                --j;
            }
            System.out.println("");
            i--;
        }   
    

    Enjoy!!

    0 讨论(0)
  • 2020-11-30 12:27
        for(int i=1;i<=5;i++)
        {
            for(int j=5;j>=i;j--)
            {
                System.out.print(" ");
            }
            for(int j=1;j<=i;j++)
            {
                System.out.print("*");
            }
    
            for(int j=1;j<=i-1;j++)
            {
                System.out.print("*");
            }
            System.out.println("");
        }
    
     *
    ***
    



    0 讨论(0)
  • 2020-11-30 12:31

    Hint: For each row, you need to first print some spaces and then print some stars. The number of spaces should decrease by one per row, while the number of stars should increase.

    For the centered output, increase the number of stars by two for each row.

    0 讨论(0)
  • 2020-11-30 12:31

    1) Normal triangle

    package test1;
    
    
     class Test1 {
    
         public static void main(String args[])
         {
             int n=5;
         for(int i=0;i<n;i++)
         {
    
             for(int j=0;j<=n-i;j++)
             {
    
                 System.out.print(" ");
             }
    
             for(int k=0;k<=2*i;k++)
             {
             System.out.print("*");
             }
    
             System.out.println("\n");
    
         }
    
         }
    

    2) right angle triangle

    package test1;
    
    
     class Test1 {
    
         public static void main(String args[])
         {
             int n=5;
         for(int i=0;i<n;i++)
         {
    
             for(int j=0;j<=n-i;j++)
             {
    
                 System.out.print(" ");
             }
    
             for(int k=0;k<=i;k++)
             {
             System.out.print("*");
             }
    
             System.out.println("\n");
    
         }
    
         }
    
    
    }
    
         }
    

    3) Left angle triangle

    package test1;
    
    
     class Test1 {
    
         public static void main(String args[])
         {
             int n=5;
         for(int i=0;i<n;i++)
         {
    
             for(int j=0;j<=i;j++)
             {
    
                 System.out.print("*");
             }
    
             System.out.println("\n");
    
         }
    
         }
    
    
    }
    
    0 讨论(0)
  • 2020-11-30 12:31

    For center triangle

          Scanner sc = new Scanner(System.in);
          int n=sc.nextInt();
          int b=(n-1)*2;  
          for(int i=1;i<=n;i++){
          int t= i;
          for(int k=1;k<=b;k++){
          System.out.print(" ");
           }
           if(i!=1){
            t=i*2-1;
           }
           for(int j=1;j<=t;j++){
           System.out.print("*");
           if(j!=t){
           System.out.print(" ");
           }
            }
            System.out.println();
                b=b-2;  
            }
    

    output:

                *
              * * *
    
    0 讨论(0)
提交回复
热议问题