Help with program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sallyk57
    New Member
    • Oct 2006
    • 13

    #1

    Help with program

    How would I make a java program using for loops to print the following pattern...

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

    i started it but dont know what to do...

    public class Stars
    {
    public static void main (String []args)
    {
    final int MAX_ROWS=10;
    for (int row=1; row<=MAX_ROWS; row++)
    {

    .....
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sallyk57
    How would I make a java program using for loops to print the following pattern...

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

    i started it but dont know what to do...

    public class Stars
    {
    public static void main (String []args)
    {
    final int MAX_ROWS=10;
    for (int row=1; row<=MAX_ROWS; row++)
    {

    .....
    Never under-estimate the power of the "growing phase-shrinking phase approach"

    Code:
    public class Stars {
    	public static void main (String []args) {
    		final int MAX_ROWS = 9;
    		int n = 6;
    		String stars = "*";
    		for (int row=0; row < MAX_ROWS; row++) {
    			for(int i = 0; i < n;i++) {
    				System.out.print(" ");
    			}
    			System.out.println(stars);
    
    			if(row >= 4) {
    				if(stars.length() == 1) {
    					break;
    				}
    				n++;
    				if(row == 9) {
    
    				}
    				else {
    					stars = stars.substring(0, stars.length()-2);
    
    				}
    			}
    			else {
    				n--;
    				stars = stars + "**";
    			}
    		System.out.println();
    		}
    	}
    }

    Comment

    Working...