3

Doing some homework in my CSC 2310 class and I can't figure out this one problem... it reads:

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 '*'. Name the program as PrintTriangle.java.

My code is pretty much blank because I need to figure out how to make the code see that when i = 100 to print out one hundred asterisks, when i = 99 print ninety-nine, etc. etc. But this is all i have so far:

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) {
        // code here that reads i's value and prints out an equal value of *
            i--;
        }
    }
}

The rest of the assignment was way more difficult than this one which confuses me that I can't figure this out. Any help would be greatly appreciated.

4

4 回答 4

2

You clearly need 100 lines as you know. So you need an outer loop that undertakes 100 iterations (as you have). In the body of this loop, you must print i * characters on a single line, so you just need:

for (int j = 0 ; j < i ; j++) System.out.print("*");
System.out.println();  // newline at the end

Hence you will have:

int i = 100;
while (i > 0) {
    for (int j = 0; j < i; j++)
        System.out.print("*");
    System.out.println();
    i--;
}

Or equivalently,

for (int i = 100 ; i > 0 ; i--) {
    for (int j = 0; j < i; j++)
        System.out.print("*");
    System.out.println();
}

EDIT Using only while loops:

int i = 100;  // this is our outer loop control variable
while (i > 0) {
    int j = 0;  // this is our inner loop control variable
    while (j < i) {
        System.out.print("*");
        j++;
    }
    System.out.println();
    i--;
}

So to break it down:

  • We have an outer loop that loops from i = 100 downwards to i = 1.

  • Inside this outer while loop, we have another loop that loops from 0 to i - 1. So, on the first iteration, this would be from 0-99 (100 total iterations), then from 0-98 (99 total iterations), then from 0-97 (98 total iterations) etc.

  • Inside this inner loop, we print a * character. But we do this i times (because it's a loop), so the first time we have 100 *s, then 99, then 98 etc. (as you can see from the point above).

  • Hence, the triangle pattern emerges.

于 2012-09-29T02:31:06.810 回答
1

You need two loops, one to determine how many characters to print on each line, and an inner nested loop to determine how many times to print a single character.

The hint is that the inner loop doesn't always count to a fixed number, rather it counts from 1 to (100 - something).

于 2012-09-29T02:35:54.967 回答
1

Try this:

public class PrintTriangle {
    public static void main(String[] args) {

       for(int i = 100; i >= 1; i--){

           System.out.print("\n");

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

       }

   }
}

Explanation: The nested for loop has a variable named j. j is the amount of times * has been printed. After printing it checks if it is equal to i. i is a variable in the big for loop. i keeps track of how many times a line has been printed. \n means newline.

于 2012-09-29T02:45:48.177 回答
0

You could come at it side ways...

StringBuilder sb = new StringBuilder(100);
int index = 0;
while (index < 100) {
    sb.append("*");
    index++;
}

index = 0;
while (index < 100) {
    System.out.println(sb);
    sb.deleteCharAt(0);
    index++;
}

But I think I prefer the loop with loop approach personally ;)

You could improve the effiency of the first loop by increasing the number of stars you add per loop and reducing the number loops accordingly...

ie, add 2 starts, need 50 loops, add 4, need 25, add 5 need 20, add 10, need 10...

For example

while (index < 10) {
    sb.append("**********");
    index++;
}
于 2012-09-29T03:27:38.040 回答