Author Topic: fun with Java!  (Read 760 times)

achtung5

  • C.H.U.M.
  • ******
  • Posts: 2461
  • don't taze me, bro!
    • achtung5's Website
fun with Java!
« on: October 09, 2008, 06:09:24 PM »
to start off... http://java.sun.com/

I'm taking a comp sci class this year and we learn Java... so i had the good idea to post a forum for discussion about java, where we could post codes we might be having trouble with, and just a general discussion of the language.

I'm going to start off by talking about how much I hate it when you accidentally create an infinite loop.

also... I was wondering if someone could help me with this code... what i have so far is as follows:

// ********************************************************************
// Stars.java              Author: achtung
//
// Prints diamond shape using asterisk (star) characters.
// ********************************************************************

public class Stars5
{
 
  public static void main (String[] args)
  {
   
    final int MAX_ROWS = 10;
   
    for (int row = 1; row <= MAX_ROWS/2; row++){
      for (int star = 1; star <= (MAX_ROWS/2) - row; star++)
        System.out.print (" ");
      for (int row2 = 1; row2 <= (row*2) -1; row2++)
        System.out.print ("*");
      System.out.println();
    }
   
    for (int row = 1; row <= (MAX_ROWS/2); row++){
      for (int star = 1; star<= row -1; star++)
        System.out.print (" ");
      for (int row2 = 1; row2 <= MAX_ROWS - (row*2) + 1; row2++)
        System.out.print ("*");
      System.out.println();
    }
  }
}

the output should read:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

but i seem to be getting it to look like this... and i want to know why!  >:(
    *
   ***
  *****
 *******
*********
*********
 *******
  *****
   ***
    *

can someone help me out?

Malthan

  • Flaming Troll
  • ***
  • Posts: 106
Re: fun with Java!
« Reply #1 on: November 23, 2008, 04:39:55 PM »
Well the shape you are trying to draw is symmetrical except for the middle line which is drawn only once, so you either need to draw it in the frst or second loop, not in both.

class ClassName {
    /**
     * Default constructor
     */
    public ClassName(){

    }
    public static void main(String args[]) {

    final int MAX_ROWS = 10;
   
    for (int row = 1; row <= MAX_ROWS/2; row++){
      for (int star = 0; star <= (MAX_ROWS/2) - row; star++)
        System.out.print (" ");
      for (int row2 = 1; row2 <= (row*2) -1; row2++)
        System.out.print ("*");
      System.out.println();
    }
   
    for (int row = 0; row <= (MAX_ROWS/2); row++){
      for (int star = 0; star<= row -1; star++)
        System.out.print (" ");
      for (int row2 = 1; row2 <= MAX_ROWS - (row*2) + 1; row2++)
        System.out.print ("*");
      System.out.println();
    }

}
}

A quick fix, but it works (at least I hope, I cheked only for the given example) - now only the first loop draws the middle line, and number of spaces have been increased to match.