Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

Please help!

Thread view: 
johnny - 09 Dec 2004 18:58 GMT
Anyone know how I could write a simple Java program using loops to get
the following output?

1
1 2
1 2 3
1 2 3 4

I'm usually pretty good with loops but can't seem to get this one.
Thanks!
Chris Smith - 09 Dec 2004 19:41 GMT
> Anyone know how I could write a simple Java program using loops to get
> the following output?
[quoted text clipped - 6 lines]
> I'm usually pretty good with loops but can't seem to get this one.
> Thanks!

It's fairly simple, but you need two nested loops.  If you can't figure
it out, please post the code to your attempt.  It's hard to help you
unless we can get some idea of what specifically you don't understand
about this task.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Stefan Schulz - 09 Dec 2004 19:53 GMT
> Anyone know how I could write a simple Java program using loops to get
> the following output?
[quoted text clipped - 6 lines]
> I'm usually pretty good with loops but can't seem to get this one.
> Thanks!

1) Create a StringBuffer (or, if using 1.5, a StringBuilder)
2) In each iteration, add the current value of the iterated variable to  
the buffer, then a space
3) print the value of the buffer in each iteration

Oh, you wanted actual code?
Signature


Whom the gods wish to destroy they first call promising.

Grant Wagner - 09 Dec 2004 20:08 GMT
> Anyone know how I could write a simple Java program using loops to get
> the following output?
[quoted text clipped - 6 lines]
> I'm usually pretty good with loops but can't seem to get this one.
> Thanks!

Here you go:

public class Lines{static java.io.PrintStream _=System.out;static int
_int(int i){int j;for(j=1;j<i;_.print(j+++" "));return j;}public static
void main(String[]s){for(int i=1;i<=4;_.println(_int(i++)));}}

Anyone who's "pretty good with loops" should have no problem figuring it
out.

I hope your instructor gives me a good mark.

--
Grant Wagner <gwagner@agricoreunited.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.