Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class DiamondCross {
- /**
- * Program: DiamondCross.java
- * Purpose: Yahoo! Answers
- * Creator: Chris Clarke
- * Created: 04.01.2014
- */
- public static void main( String args[] ) {
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter value of n: ");
- int n;
- do {
- n=scan.nextInt();
- if (n%2==0) System.out.println("Must be an odd number!");
- } while (n%2==0);
- int sp=20; // initialise variable sp to the integer value 20
- /* print upper half of diamond:
- decreasing spaces and increasing stars
- */
- // loop from 1 to 9, increasing by 2 each iteration
- for(int r = 1; r <= n; r += 2) {
- // loop from 1 to the value of sp
- for(int i = 1; i <= sp ; i++)
- // output a space to screen
- System.out.print(" ");
- // loop from 1 to the value of r
- for(int c = 1; c <= r; c++) {
- if (c==1 || c==r/2+1 || c==r || r==n) {
- // output a star to screen
- System.out.print("*");
- } else {
- // output a star to screen
- System.out.print(" ");
- } // end if
- } // end for
- // output a new line
- System.out.println();
- // the variable sp is decremented by 1
- --sp;
- }//end for r
- /* print lower half of diamond:
- increasing spaces and decreasing stars
- */
- sp+=2;
- // loop from 7 to 1, decreasing by 2 each iteration
- for(int r = n-2; r >= 1; r -= 2) {
- // loop from 1 to the value of space
- for(int i = 1;i <= sp; i++)
- // output a space to screen
- System.out.print(" ");
- // loop from 1 to the value of r
- for(int c =1;c <= r; c++) {
- if (c==1 || c==r/2+1 || c==r) {
- // output a star to screen
- System.out.print("*");
- } else {
- // output a star to screen
- System.out.print(" ");
- } // end if
- } // end for
- // output a new line
- System.out.println();
- // the variable space is incremented by 1
- sp++;
- }//end for r
- }//end main()
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement