By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,032 Members | 825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,032 IT Pros & Developers. It's quick & easy.

simple java problem

P: 36
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
Jan 29 '07 #1
Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
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?
Jan 29 '07 #2

P: 36
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
Jan 29 '07 #3

P: 36
for(int i=1;i<=4;i++)
{
for(int k=1;k<=i;k++)
{
System.out.print(k);
for(int j=k ; j!=1 && k==i && k!=1;)
System.out.print(--j);
}

System.out.print("/n");
}


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

System.out.print("/n");
}
Jan 29 '07 #4

P: 36
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
Jan 29 '07 #5

10K+
P: 13,264
for(int i=1;i<=4;i++)
{
for(int k=1;k<=i;k++)
{
System.out.print(k);
for(int j=k ; j!=1 && k==i && k!=1;)
System.out.print(--j);
}

System.out.print("/n");
}


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

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

Up then down

Expand|Select|Wrap|Line Numbers
  1. class DiamondNumbers {
  2.      public static void main(String[] args) {
  3.          int n = 4;
  4.          int i = 1;
  5.          while(i < 5) {
  6.              for(int k = 0; k < n;k++) {
  7.                  System.out.print(" ");
  8.              }
  9.                 for(int l = 1; l < i; l++) {
  10.                  System.out.print(""+l);
  11.              }
  12.              for(int l = i; l > 0; l--) {
  13.                    System.out.print(""+l);
  14.              }
  15.              i++;
  16.              n--;
  17.              System.out.println();
  18.          }
  19.  
  20.          //Reverse
  21.          i = 3;
  22.          n = 2;
  23.          while(i > 0) {
  24.               for(int k = 0; k < n;k++) {
  25.                   System.out.print(" ");
  26.               }
  27.              for(int l = 1; l < i; l++) {
  28.                   System.out.print(""+l);
  29.               }
  30.               for(int l = i; l > 0; l--) {
  31.                     System.out.print(""+l);
  32.               }
  33.               i--;
  34.               n++;
  35.               System.out.println();
  36.          }
  37.     }
  38. }

Up and down combined (sort of)


Expand|Select|Wrap|Line Numbers
  1. class DiamondNumbers {
  2.      public static void main(String[] args) {
  3.          int n = 4;
  4.          int i = 1;
  5.          int x = -1;
  6.          boolean up = true;
  7.  
  8.          while((i < 6 && up) || (i > 0 && !up)) {
  9.              for(int k = 0; k < n;k++) {
  10.                  System.out.print(" ");
  11.              }
  12.                 for(int l = 1; l < i; l++) {
  13.                  System.out.print(""+l);
  14.              }
  15.              for(int l = i; l > 0; l--) {
  16.                    System.out.print(""+l);
  17.              }
  18.              if(up) {
  19.                  i++;
  20.                   n--;
  21.              }
  22.              else {
  23.                  i--;
  24.                  n++;
  25.              }
  26.              System.out.println();
  27.              if(i == 5) {
  28.                  up = false;
  29.                  n = 2;
  30.                  i = 3;
  31.              }
  32.          }
  33.  
  34.     }
  35. }
Jan 29 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.