Results 1 to 1 of 1
Thread: Diamond in java
- 11-17-2011, 03:27 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 1
- Rep Power
- 0
Diamond in java
Hi i am trying to complete this task:
Write an a Java application which prints a diamond in a
square grid of dots whose side length is input to the
application. The side of the square must be an odd
number. If the input data is invalid an error message is
output. For example, if the input data was 10 then the
error message
Size (10) invalid must be odd
would be printed. If the input data was -5 then the error
message
Sides of square must be positive
would be printed like this:
..*..
.*.*.
*...*
.*.*.
..*..
any help would be greatly appreciated here is the code I already have the following code:
class Main
{
int i , j , k, n = 5; //n is the number of lines and i,j are used for loops
k=(n==0)?n-1:n; //k is used to control the number of spaces to be printed
for( i = 0 ; i < n/2 ; i++ ) //for the top portion of the diamond
{
for( j = 0 ; j < k / 2 - i ; j++ ) //print required spaces
System.out.print(" ");
for ( j = 0 ; j < i * 2 + 1 ; j++ ) //print required stars
System.out.print("*");
System.out.println(); //line break after the end of a line
}
if( n % 2 != 0) //if the number of lines is odd
{
for( i = 0 ; i < n ; i++ ) //print extra asterisks in the middle
System.out.print("*");
System.out.println(); //line break
}
for( i = n/2-1 ; i >= 0 ; i-- ) //for the bottom portion of the diamond
{
for( j = 0 ; j < k/2 - i ; j++ ) //print required spaces
System.out.print(".");
for( j = 0 ; j < i*2+1 ; j++ ) //print required stars
System.out.print("*");
System.out.println(); //line break after the end of a line
}
} //end main
} //end class diamond
Similar Threads
-
ASCII art, drawing an asterisks diamond within dots.
By ajknight7498 in forum New To JavaReplies: 3Last Post: 11-11-2011, 12:20 AM -
Diamond Program?????
By Cubba27 in forum New To JavaReplies: 3Last Post: 11-19-2009, 09:15 PM
Bookmarks