简体   繁体   中英

beginner java - printing a right triangle backwards

last time i was on here, the only question i needed help with in my computer science homework involved making a right triangle on 100 lines, here was the code for that:

    public class PrintTriangle {
   public static void main(String[] args) {
      // Print a right triangle made up of *
      // starting at 100 and ending with 1
         int i = 100;
         while (i > 0) {
           for (int j = 0; j < i; j++)
           System.out.print("*");
           System.out.println();
         i--;
         }
   }
}

well now he's asked us to do the inverse. here's the actual question:

"Write a program that will draw a right triangle of 100 lines in the following shape: The first line, print 100 ' ', the second line, 99 ' '... the last line, only one '*'. Using for loop for this problem. Name the program as PrintTriangle.java"

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

i'm sure it's something simple but everything i've tried up to this point has flopped or only created 1 space at a time. any suggestions or help would be greatly appreciated! thank you in advance!

OK, first take a look on both the problems. How would you relate them.

Since the second problem is the reverse of the first, so what you did first in your first code, you need to do that last in this next problem..

So, your loop should actually work backwards where it ended in the below code.

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

So, think what you need to do to make this loop work backwards.

Hint : -

  • Incrementing from 0 to 100 is forward
  • Decrementing from 100 to 0 is backwards

     **** *** ** * 

Also, in your above pattern you see that you need to first print spaces before actually printing your characters So, that too you need to consider.

So, here you have to actually print two different characters: -

  • Few Spaces , followed by,
  • Few *'s .

Here's the pattern: -

  • Let's the maximum row is max (100 in your case)
  • Row ( i ) has ( i ) number of spaces (Row 0 has 0 space, Row 1 has 1 space)
  • Then it has ( n - i ) number of stars (Row 0 has 100 stars, Row 1 has 99 stars)

So, you can see that you actually need two loops here.

Analyze what all I said, and come up with some code. Try it out.

public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < i; j++)
                System.out.print(" ");
            for (int j = i; j < 100; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

I am just giving you idea how you can do it. Understand the pattern.

Just like printed * on previous question you need to print spaces. Then print stars on reverse order.

*
**
***

***
 **
  *

For these kind of pyramids (what i call them) first make sure you get the spaces right

* * * * *
- * * * *
- - * * *
- - - * * 
- - - - *

Now you can see two patterns and now you can program it. Here is psuedocode...

FOR I=1 to N
    FOR J = 1 to I-1
        PRINT " "
    ENDFOR
    FOR K = 1 to N-I+1
        PRINT "*"
    ENDFOR
PRINT "\n"
ENDFOR
 int i = 1;
 while (i =< 100) {
   // first put spaces as long as it is necessary
   // it will be i times less than 100
   // for example for the first line (i = 1), 99 space (100-i) and 1 star (i)
   // for the 50. line (i == 50), 50 space (100-i) and 50 stars (i)
   for(int j = 0; j < 100-i; j++)
     System.out.print(" ");

   // then the stars
   for (int j = 0; j < i; j++)
     System.out.print("*");
   System.out.println();
   i++;
 }

The code below can help you to find solution.

 class ReverseTriangle {
 public static void main(String[] args) {
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < i; j++)
            System.out.print(" ");
        for (int j = i; j < 100; j++)
            System.out.print("*");
        System.out.println();
    }
 }
 }
public class PrintTriangle {
   public static void main(String[] args) {

       for(int i=10;i>0;i--)
          {
           for (int j = 1; j < i; j++)
           System.out.print("*");
           System.out.println();           
         }
   }
}
public static void printPyramid(int ln){
    for(int j=ln;j>0;j--){
        for(int i=ln;i>0;i--) {
            if (j >= i) {
                System.out.print("*");
            } else {
            }
            System.out.print(" ");
        }
        System.out.println();
    }
}

public static void printReversePyramid(int ln){
    for(int j=0;j<ln;j++){
        for(int i=ln;i>=0;i--) {
            if (j >= i) {
                System.out.print("*");
            } else {
            }
            System.out.print(" ");
        }
        System.out.println();
    }
}

public static void printDiamond(int ln){
    for(int j=0;j<2*ln+1;j++){
        for(int i=ln;i>=0;i--) {
            if (j >= i && j < ln+1) {
                System.out.print("*");
            } else if(j > ln && (2*ln)-j>=i){
                System.out.print("*");
            }
            else {
            }
            System.out.print(" ");
        }
        System.out.println();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM