Home > Mobile >  Take in a positive integer n Create n triangles in stars with their bases below of size n each
Take in a positive integer n Create n triangles in stars with their bases below of size n each

Time:01-26

Help with this question: Take a positive integer n and form n triangles from stars with their base down of size n each. For example, for input 3, the following output will be obtained:

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

Here's what I've tried.

import java.util.Scanner;

public class ex3 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("enter n");
        int n = input.nextInt();
        while (n <= 0) {
            System.out.println("error");
            n = input.nextInt();
        }
        for (int r = 1; r <= n; r  ) {
            for (int c = 1; c <= n - r; c  )
                System.out.print(" ");
            for (int c = 1; c <= r; c  )
                System.out.print("*");
            System.out.println();
        }
    }
}

CodePudding user response:

Here's my solution, with explanations in comments

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
        try(Scanner input = new Scanner(System.in)) {
          System.out.println("enter n");
          int n = input.nextInt();
          while (n <= 0) {
              System.out.println("error");
              n = input.nextInt();
          }
          for (int r = 1; r <= n; r  ) { // <-- we will have to print n rows
              printLine(n, r); 
          }
        }
    }

    static void printLine(int n, int lineNumber) {
      StringBuffer line = new StringBuffer();
      for(int i = 0; i < n; i   ) { // <-- each line will have '*'s for n triangles
        for(int j = lineNumber; j > 0; j--) { // <-- each line has as many '*'s as its line number, so print those first
          line.append("*");
        }
        for(int j = 0; j < n - lineNumber   1; j   ) { // <-- we then have to add enough spaces to leave room for the next triangle's '*'s
          line.append(" ");
        }
      }
      System.out.println(line.toString()); // <-- print the line we've built so far
    }
}

EDIT: Here's a replit that avoids one of the loops and also uses recursion in place of the outer-most loop: https://replit.com/@anqit/MicroExtrovertedTrace#Main.java

CodePudding user response:

First, the blanks follow the stars, then, you have to repeat n times the two loops:

    for (int r = 1; r <= n; r  ) {
        for (int t = 1; t <= n; t  ) {
            for (int c = 1; c <= r; c  )
                System.out.print("*");
            for (int c = 1; c <= n - r; c  )
                System.out.print(" ");
        }
        System.out.println();
    }

CodePudding user response:

You have one for loop that prints out a single triangle of stars using nested for loops. The outer loop is responsible for the number of rows and the inner loops are responsible for printing the spaces and stars.

In my updated code, I added an outer loop that runs n times, and each time it runs, it prints out a triangle of stars. The inner loops are responsible for printing the stars of the triangle and spaces between the triangles.

The main difference is that in your first code, only one triangle is printed, while in my updated code, n triangles are printed. In addition, the indentation of the inner loops has been adjusted to align the triangles correctly on the same line.

import java.util.Scanner;

public class ex3 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("enter n:");
        int n = input.nextInt();
        while (n <= 0) {
            System.out.println("error: please enter a positive number:");
            n = input.nextInt();
        }
       for (int r = 1; r <= n; r  ) {
            for (int t = 1; t <= n; t  ) {
                for (int c = 1; c <= r; c  ) {
                    System.out.print("*");
                }
                for (int i = 0; i < n - r   1; i  ) {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

CodePudding user response:

Here is a 3 forloop variant

int n = 4;
for (int i = 1; i <= n; i  ) {
    StringBuilder b = new StringBuilder("");
    for (int s = 0; s < n; s  ) {
        b.append(s < i ? "*" : " ");
    }
    String line = b.append(" ").toString();//space between
    for (int j = 1; j < n; j  ) {
        System.out.print(line);
    }
    System.out.println();
}

produces

> *    *    *    
> **   **   **   
> ***  ***  ***  
> **** **** ****
  •  Tags:  
  • java
  • Related