Claudia Gomes
Claudia Gomes

Reputation: 41

Using nested for loops to create a triangle with asterisks

using nested for loops statements to draw triangles of ""s. The number of ""s on the last row is input from the user (valid range: 5 to 21). the out put should look like this: Sample output:

How many stars/last row (5-21)? 25 Out of range. Reenter: 7

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

so far this is what i have for the code. I don't know how to get it to look like a triangle. any help would be great.

import java.util.Scanner;

public class Lab7_2{
  public static void main(String[] args){
    //declarations
    Scanner input= new Scanner(System.in);
    int how_many;//num of triangles
    int m; //constant
    int c;//constant
    int rows;//row

    //prompt for input
    System.out.println("Drawing triangles program.");
    System.out.println("==============================");
    System.out.println("How many triangles?");
    how_many=input.nextInt();
    System.out.println("How many stars/last row (5-21)?");
    rows=input.nextInt();
    while(rows<=5||rows>=21){
      System.out.println("Out of range. Reenter: ");
      rows=input.nextInt();
    }
    for(m=1;m<=rows;m++){
      for(c=1;c<=m;c++){
        System.out.println("*");
        System.out.println();
    }
  }
}
}

Upvotes: 2

Views: 9988

Answers (5)

ThaBomb
ThaBomb

Reputation: 722

I believe this is the most efficient and simple way to do it, saves having to call the print/println method hundreds of times when playing with larger pyramids.

String out;
for (m = 0; m < rows; m++) {
    out = "";
    for (c = 0; c < m; c++) {
        out+="*";
        System.out.println(out);
    }
}

Basicly your string is "", and you add a "*" after every time you print it to the next line.

Upvotes: 0

AlexR
AlexR

Reputation: 2408

To center a line, use this:

private static String center(String line, int length) {
    StringBuilder newLine = new StringBuilder();
    for (int i = 0; i < (line.length() - length)/2; i++)
        newLine.append(" ");
    }
    newLine.append(line);
    return newLine.toString();
}

Also,

System.out.println();

prints a line break after each string, which is not what you intend.


Fixed code:

private void printTriangle(int base) {
    StringBuilder currentStars = new StringBuilder();
    for (int currLine = 1; currLine < base; currLine++) {
        currentStars.append("*"); // Don't create a new String, just append a "*" to the old line.
        //if (currLine % 2 == 1)
        //    System.out.println(center(currentStars.toString(), base)); // For centered triangle
        System.out.println(currentStars.toString()); // For non-centered triangle
    }
}

Upvotes: 2

Richard Tingle
Richard Tingle

Reputation: 17226

You are using a println statement to print your stars, as such each will be on its own line no matter what

System.out.println("*");

You want a print statement instead

System.out.print("*");

Additionally inside the star printing loop you have an extra System.out.println(); putting a blank line in, that should be outside the inner for loop

for(m=1;m<=rows;m++){
  for(c=1;c<=m;c++){
    System.out.println("*"); <-- println always starts a new line, should be print
    System.out.println(); <--- shouldn't be within inner loop
  }
  <--- println() should be here to advance to the next line of stars
}

Upvotes: 1

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Just fix your for loop as

for (m = 1; m <= rows; m++) {
    for (c = 1; c <= m; c++) {
        // first print the * on the same line
        System.out.print("*");
    }
    // then move to the next line
    System.out.println();
}

Notice, that you need to use System.out.print() (that does not write a new line \n to the output stream) for the asterisks * to get printed on the same line.

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328526

println() always starts a new line after the output. Try print instead and then one println() after the inner loop.

Upvotes: 0

Related Questions