Advertisement
brilliant_moves

DiamondCross.java

Jan 4th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.94 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class DiamondCross {
  4.  
  5.     /**
  6.     *   Program:    DiamondCross.java
  7.     *   Purpose:    Yahoo! Answers
  8.     *   Creator:    Chris Clarke
  9.     *   Created:    04.01.2014
  10.     */
  11.  
  12.     public static void main( String args[] ) {
  13.  
  14.         Scanner scan = new Scanner(System.in);
  15.         System.out.println("Enter value of n: ");
  16.         int n;
  17.         do {
  18.             n=scan.nextInt();
  19.             if (n%2==0) System.out.println("Must be an odd number!");
  20.         } while (n%2==0);
  21.  
  22.         int sp=20;  // initialise variable sp to the integer value 20
  23.  
  24.         /*  print upper half of diamond:
  25.             decreasing spaces and increasing stars
  26.         */
  27.  
  28.         // loop from 1 to 9, increasing by 2 each iteration
  29.         for(int r = 1; r <= n; r += 2) {
  30.             // loop from 1 to the value of sp
  31.             for(int i = 1; i <= sp ; i++)
  32.                 // output a space to screen
  33.                 System.out.print(" ");
  34.  
  35.             // loop from 1 to the value of r
  36.             for(int c = 1; c <= r; c++) {
  37.                 if (c==1 || c==r/2+1 || c==r || r==n) {
  38.                     // output a star to screen
  39.                     System.out.print("*");
  40.                 } else {
  41.                     // output a star to screen
  42.                     System.out.print(" ");
  43.                 } // end if
  44.             } // end for
  45.             // output a new line
  46.             System.out.println();
  47.             // the variable sp is decremented by 1
  48.             --sp;
  49.         }//end for r
  50.  
  51.         /*  print lower half of diamond:
  52.             increasing spaces and decreasing stars
  53.         */
  54.  
  55.         sp+=2;
  56.  
  57.         // loop from 7 to 1, decreasing by 2 each iteration
  58.         for(int r = n-2; r >= 1; r -= 2) {
  59.             // loop from 1 to the value of space
  60.             for(int i = 1;i <= sp; i++)
  61.                 // output a space to screen
  62.                 System.out.print(" ");
  63.  
  64.             // loop from 1 to the value of r
  65.             for(int c =1;c <= r; c++) {
  66.                 if (c==1 || c==r/2+1 || c==r) {
  67.                     // output a star to screen
  68.                     System.out.print("*");
  69.                 } else {
  70.                     // output a star to screen
  71.                     System.out.print(" ");
  72.                 } // end if
  73.             } // end for
  74.             // output a new line
  75.             System.out.println();
  76.             // the variable space is incremented by 1
  77.             sp++;
  78.         }//end for r
  79.     }//end main()
  80. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement