473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
Jan 29 '07 #1
5 1571
r035198x
13,262 8TB
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
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
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
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
r035198x
13,262 8TB
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

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

Similar topics

0
by: Hal Vaughan | last post by:
I'm working with javax.mail.*. I have no problem with reading in messages. I'm not using multi-part messages or anything, I just use this setup: Session oSession =...
10
by: martin | last post by:
Hello, I just got the SUN Java IDE. (Netbeans IDE 3.5.1) Very very nice, and I worked myself through the tutorial (about making a colorswitch). Now, When I compile it gives no errors at all. So...
0
by: Ali | last post by:
I'm trying to write a really basic chat program that allows 2 client programs to send messages to each other via a server. I've managed to write the code so that both clients can connect to the...
4
by: Stefan Poehn | last post by:
Hi does anybody know a tool that converts Java-Code to C++? I need a very simple converter that does NOT need to cover Threading, Swing, Exceptions, Reflection, RMI, Security, Networking and...
4
by: Owen Parker | last post by:
Hi all I am not a java programmer but i can hack at it a bit. I am trying to allow a user to define the text in a simple javascript text scroller. The data is stored as multiple records in a...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
7
by: imatts | last post by:
Hi can anyone help with this little problem. I have a simple script to swap between two divs on a page. It works perfectly in Firefox & Safari & Opera. It fails in IE 6 giving Object Required error...
6
Nepomuk
by: Nepomuk | last post by:
Hi everyone! Until now, I've almost only programmed in Java and am now learning C++ (using Bjarne Stroustrup's book). Now, I wanted to test one of the examples from the book, but somehow it doesn't...
5
by: sayeo87 | last post by:
Hi, I am quite new to JSP so please forgive me if I ask really simple things... I am trying to run system commands on the server and display the output on a webpage. This is what I've got: <%@...
5
by: Ibys | last post by:
Hi, i am just starting to learn javascript, so i am probably doing something very simple wrong. i have read a lot of articles on maths in java, but cant find anything simple enough for my problem. I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.