•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,335 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 5,217 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 243 | Replies: 5 | Solved
![]() |
Post your code and we will help you correct it, but no one is just going to give it to you.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
As you can see for each line, you have a start, then increase each element by 1 and when you reach 5, if you didn't insert 5 elements in the line start reducing.
5 4 3 2 1(start)
4 5(reached 5, will decrease until the line is filled) 4 3 2(start)
3 4 5(reached 5, will decrease until the line is filled) 4 3(start)
5 4 3 2 1(start)
4 5(reached 5, will decrease until the line is filled) 4 3 2(start)
3 4 5(reached 5, will decrease until the line is filled) 4 3(start)
I AM the 12th CYLON
•
•
Join Date: Feb 2008
Posts: 33
Reputation:
Rep Power: 1
Solved Threads: 0
Here's my code..
it doesnt do much...yet
it doesnt do much...yet
public class LogicPuzzle1 {
public static void main (String [] args)
{
final int num = 5;
int ctr;
int ctr2;
int temp=0;
for(ctr=num;ctr>0;ctr--)
{
System.out.print(ctr);
if(ctr!=num)
{
temp=ctr;
if(temp==num)
{
for(ctr2=1;ctr2<num;ctr2++)
{
temp--;
System.out.print(temp);
}
}
else
{
for(ctr2=1;ctr2<num;ctr2++)
{
temp++;
System.out.print(temp);
}
}
}
else
{
for(ctr2=1;ctr2<num;ctr2++)
System.out.print(ctr-ctr2);
}
System.out.println();
}
}
}•
•
Join Date: Feb 2008
Posts: 33
Reputation:
Rep Power: 1
Solved Threads: 0
THANK YOU javaAddict!!!!!!! Weeeee....
can anyone help me make my code more efficient? (remove uneeded stuff)
can anyone help me make my code more efficient? (remove uneeded stuff)
public class LogicPuzzle1 {
public static void main (String [] args)
{
final int num = 5;
int ctr;
int ctr2;
int temp=0;
boolean reachMax=false;
for(ctr=num;ctr>0;ctr--)
{
ctr2=num;
temp=ctr-1;
reachMax=false;
while(ctr2>0)
{
if(temp==5||reachMax==true)
{
reachMax=true;
temp--;
}
else
{
temp++;
}
System.out.print(temp);
ctr2--;
}
System.out.println();
}
}
} There are many ways to implement this, so if it works don't worry about it. In my idea I was thinking of creating an array 5x5, and put the values in it before printing them. I think that it would have been simpler because with the array I could start putting things at the end of the array:
arr[0][4] = 1
arr[0][3] = 2 . . .
So instead of while inside the first for-loop, I would have the outside loop counting the line of the array, and the inside loop counting backwards and start putting the values at the end of the array.
arr[0][4] = 1
arr[0][3] = 2 . . .
So instead of while inside the first for-loop, I would have the outside loop counting the line of the array, and the inside loop counting backwards and start putting the values at the end of the array.
I AM the 12th CYLON
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- non blocking getchar.. big problem :( (C)
- BIG problem with STL List Container (C)
- internet explorer / virus problem here (Viruses, Spyware and other Nasties)
- Big Problem (Viruses, Spyware and other Nasties)
- BIG problem... laptop screen shaking/whiting out! (Monitors, Displays and Video Cards)
- Big Problem!!!!!!!!!!! (Viruses, Spyware and other Nasties)
- Big Problem, Generic Error (VB.NET)
- Big Problem (system freezing) (Troubleshooting Dead Machines)
- CommonName big problem (Windows NT / 2000 / XP / 2003)
Other Threads in the Java Forum
- Previous Thread: Passing Value to other JSP
- Next Thread: JVM-java vitual machine



Linear Mode