simple java problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • analoveu
    New Member
    • Jan 2007
    • 36

    simple java problem

    I need a piece of code that solve the following problem:
    I wanna print this shape

    1
    1 2 1
    1 2 3 2 1
    1 2 3 4 3 2 1
    1 2 3 2 1
    1 2 1
    1
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by analoveu
    I need a piece of code that solve the following problem:
    I wanna print this shape

    1
    1 2 1
    1 2 3 2 1
    1 2 3 4 3 2 1
    1 2 3 2 1
    1 2 1
    1
    What have you tried so far?

    Comment

    • analoveu
      New Member
      • Jan 2007
      • 36

      #3
      some one help me ,just I need the code that perform this problem
      I thought about it ,I develped the code that solve the upper part but it wasn't logical solution

      Comment

      • analoveu
        New Member
        • Jan 2007
        • 36

        #4
        for(int i=1;i<=4;i++)
        {
        for(int k=1;k<=i;k++)
        {
        System.out.prin t(k);
        for(int j=k ; j!=1 && k==i && k!=1;)
        System.out.prin t(--j);
        }

        System.out.prin t("/n");
        }


        for(i=i-1;i!=1;i--)
        {
        for(k=1;k<=i;k+ +)
        {
        System.out.prin t(k);
        for(j=k;j!=1&&k ==i-1&&k!=1;)
        System.out.prin t(--j);
        }

        System.out.prin t("/n");
        }

        Comment

        • analoveu
          New Member
          • Jan 2007
          • 36

          #5
          the part of the bold code it perform the upper part , but the remainder of the code won't perform because the next for won't take the value of the first for ,so it will not solve the problem

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by analoveu
            for(int i=1;i<=4;i++)
            {
            for(int k=1;k<=i;k++)
            {
            System.out.prin t(k);
            for(int j=k ; j!=1 && k==i && k!=1;)
            System.out.prin t(--j);
            }

            System.out.prin t("/n");
            }


            for(i=i-1;i!=1;i--)
            {
            for(k=1;k<=i;k+ +)
            {
            System.out.prin t(k);
            for(j=k;j!=1&&k ==i-1&&k!=1;)
            System.out.prin t(--j);
            }

            System.out.prin t("/n");
            }
            Next time you post code please use code tags.

            Up then down

            Code:
            class DiamondNumbers {
                 public static void main(String[] args) {
            		 int n = 4;
            		 int i = 1;
            		 while(i < 5) {
            			 for(int k = 0; k < n;k++) {
            			 	System.out.print(" ");
            			 }
            	   	     for(int l = 1; l < i; l++) {
            			 	System.out.print(""+l);
            			 }
            			 for(int l = i; l > 0; l--) {
            			   	System.out.print(""+l);
            			 }
            			 i++;
            			 n--;
            			 System.out.println();
            		 }
            
            		 //Reverse
            		 i = 3;
            		 n = 2;
            		 while(i > 0) {
            		 	 for(int k = 0; k < n;k++) {
            		 	 	System.out.print(" ");
            		 	 }
            		     for(int l = 1; l < i; l++) {
            		 	 	System.out.print(""+l);
            		 	 }
            		 	 for(int l = i; l > 0; l--) {
            		 	   	System.out.print(""+l);
            		 	 }
            		 	 i--;
            		 	 n++;
            		 	 System.out.println();
            		 }
            	}
            }

            Up and down combined (sort of)


            Code:
            class DiamondNumbers {
                 public static void main(String[] args) {
            		 int n = 4;
            		 int i = 1;
            		 int x = -1;
            		 boolean up = true;
            
            		 while((i < 6 && up) || (i > 0 && !up)) {
            			 for(int k = 0; k < n;k++) {
            			 	System.out.print(" ");
            			 }
            	   	     for(int l = 1; l < i; l++) {
            			 	System.out.print(""+l);
            			 }
            			 for(int l = i; l > 0; l--) {
            			   	System.out.print(""+l);
            			 }
            			 if(up) {
            				 i++;
            			 	 n--;
            			 }
            			 else {
            				 i--;
            				 n++;
            			 }
            			 System.out.println();
            			 if(i == 5) {
            				 up = false;
            				 n = 2;
            				 i = 3;
            			 }
            		 }
            
            	}
            }

            Comment

            Working...