Results 1 to 4 of 4
- 11-10-2011, 06:50 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
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; } } } } } }
#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
- 11-10-2011, 07:31 PM #2
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?
- 11-10-2011, 08:41 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
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.
- 11-10-2011, 11:20 PM #4
Similar Threads
-
remove all punctuation except dots hyphens and spaces with a regular expression
By Daedalus in forum Advanced JavaReplies: 2Last Post: 01-05-2011, 06:16 PM -
Drawing dots.
By hellolleh in forum NetBeansReplies: 0Last Post: 03-16-2010, 08:09 PM -
Diamond Program?????
By Cubba27 in forum New To JavaReplies: 3Last Post: 11-19-2009, 08:15 PM -
Help with ASCII Drawing--For loops--if/else
By alice.li.mastriano in forum New To JavaReplies: 1Last Post: 09-13-2009, 12:31 AM
Bookmarks