Jason37 Jason37 - 1 month ago 9
Java Question

How do i make it so that spaces generate so the outcome is different

I would like the output to have 9 spaces then a star, 8 spaces then two stars, 7 spaces and 3 stars and so on until 10 stars at the bottom

How could i generate the spaces?

import java.util.Scanner;

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

final int max_rows = 1;

for (int row = 10; row >= max_rows; row--)
{
for (int star = 1; star <= row; star++)
System.out.print ("*");

System.out.println();
}

}
}

Answer

try this

public class ThreeDotTwelvea {

    final static int NO_OF_ROWS = 10;

    public static void main(String[] args) {

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

OUTPUT

Output