Connecting Tech Pros Worldwide Help | Site Map

Help with program

Newbie
 
Join Date: Oct 2006
Posts: 13
#1: Oct 21 '06
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++)
{

.....
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Oct 23 '06

re: Help with program


Quote:

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"

Expand|Select|Wrap|Line Numbers
  1. public class Stars {
  2.     public static void main (String []args) {
  3.         final int MAX_ROWS = 9;
  4.         int n = 6;
  5.         String stars = "*";
  6.         for (int row=0; row < MAX_ROWS; row++) {
  7.             for(int i = 0; i < n;i++) {
  8.                 System.out.print(" ");
  9.             }
  10.             System.out.println(stars);
  11.  
  12.             if(row >= 4) {
  13.                 if(stars.length() == 1) {
  14.                     break;
  15.                 }
  16.                 n++;
  17.                 if(row == 9) {
  18.  
  19.                 }
  20.                 else {
  21.                     stars = stars.substring(0, stars.length()-2);
  22.  
  23.                 }
  24.             }
  25.             else {
  26.                 n--;
  27.                 stars = stars + "**";
  28.             }
  29.         System.out.println();
  30.         }
  31.     }
Reply