Results 1 to 4 of 4
  1. #1
    ajknight7498 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default ASCII art, drawing an asterisks diamond within dots.

    Hi all, new to the forum here. Currently doing a first year in a Computing course and have started learning my first programming language which is Java. Currently onto my second group of assignments and have been instructed to create a program which will drawn a diamond based on the width of a square. The number entered must be an odd number which i have already done. Here is my code and il explain the problem below:


    Java Code:
    public class Main
    {
        public static void main(String args[])
        {
            System.out.println("#Size Of Square:");
            int sizeofsquare = 7;
            int down = 2;
            int across = 1;
            int dots = 0;
            int btmacross = 1;
            int stars = sizeofsquare / 2;
            int starn = sizeofsquare / 2;
    
            if ( sizeofsquare < 0 ){
                System.out.print( "Sides of square must be positive" );
    
            }
            if ( sizeofsquare >= 0 ){
    
                if (sizeofsquare % 2 == 0) {
    
                    System.out.print( "Size " );
                    System.out.print( "(" );
                    System.out.print( sizeofsquare );
                    System.out.print(") invalid must be odd" );
                }
    
                if (sizeofsquare % 2 != 0) {
    
                    while ( sizeofsquare <= 40 ){
    
                        while ( across < sizeofsquare ){
    
                            System.out.print("."); 
    
                            if ( across == stars ){
                                System.out.print("*");
                            }                 
    
                            across = across + 1;
    
                        }
                        while ( down < sizeofsquare ){
    
                            stars = stars - 1;
                            starn = starn + 1;
    
                            System.out.println();
    
                            while ( dots < sizeofsquare ){
                                if ( dots == stars ){
    
                                    System.out.print("*");
                                    dots = dots + 1;
                                }
                                if ( dots == starn ){
                                    System.out.print("*");
                                    dots = dots + 1;
                                }
                                if ( dots < sizeofsquare ){
                                    System.out.print(".");
                                }
                                dots = dots + 1;
                            }
                            if ( down > sizeofsquare / 2){
    
                                stars = sizeofsquare / 2;
                                starn = sizeofsquare / 2;
                                    
                                if ( dots == stars ){
                                    System.out.print("*");
                                    dots = dots + 1;
                                }
                                if ( dots == starn ){
    
                                    System.out.print("*");
                                    dots = dots + 1;
                                }
    
                                if (dots < sizeofsquare ){
                                    System.out.print(".");
                                }
                                dots = dots + 1;
                            }
    
                            down = down + 1;
                            dots = 0;
    
                        }
                        while (down == sizeofsquare){
    
                            System.out.println();
    
                            while ( btmacross < sizeofsquare ) {
                                System.out.print(".");
    
                                if ( btmacross == sizeofsquare / 2 ){
                                    System.out.print("*");
                                }
    
                                btmacross = btmacross + 1;
    
                            }
    
                            down = down + 1;
                        }
                    }
                }
            }
        }
    }
    This code outputs:

    #Size Of Square:
    ...*...
    ..*.*..
    .*...*.
    *.....*
    ..*.*..
    ..*.*..
    ...*...

    however it obviously needs to be:

    #Size Of Square:
    ...*...
    ..*.*..
    .*...*.
    *.....*
    .*...*.
    ..*.*..
    ...*...

    Thanks for any help :)
    Last edited by Norm; 11-10-2011 at 07:29 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: ASCII art, drawing an asterisks diamond within dots.

    Your logic seems not to put out enough inside .s on some lines. How do you determine how many dots to put out on each line?
    None on first
    1 on 2nd
    3 on 3rd
    5 on 4th
    then reduce the number for the rest back to none.

    What algorithm are you using to determine how many inside dots to put out on each line?

  3. #3
    ajknight7498 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: ASCII art, drawing an asterisks diamond within dots.

    I'm confused, from the results shown my program is outputting enough "."s but isn't putting the *'s below the middle of the diamond in the right place due to the starn and stars variables being the same. They both need to increase/decrease like before but i can't get it to do that as i have to redeclare the variables as sizeofsquare / 2 instead of using that area to have them change e.g stars = stars - 1 or whatever.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: ASCII art, drawing an asterisks diamond within dots.

    What is your logic to determine how to format what is on a line? How does the line number relate to what is printed on each line?
    What is your algorithm for determining what goes on each line?

Similar Threads

  1. Replies: 2
    Last Post: 01-05-2011, 06:16 PM
  2. Drawing dots.
    By hellolleh in forum NetBeans
    Replies: 0
    Last Post: 03-16-2010, 08:09 PM
  3. Diamond Program?????
    By Cubba27 in forum New To Java
    Replies: 3
    Last Post: 11-19-2009, 08:15 PM
  4. Help with ASCII Drawing--For loops--if/else
    By alice.li.mastriano in forum New To Java
    Replies: 1
    Last Post: 09-13-2009, 12:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •