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 / October 2006

Tip: Looking for answers? Try searching our database.

Need help with loop

Thread view: 
meena.desani - 30 Oct 2006 01:34 GMT
why isn't the second part of my triangle not working?
import java.util.*;

public class triangle
{

public static void main(String[] args)
{
    int nLines = 5;
for (int row = 1; row <= nLines; row++)
{
    for (int star= 1; star <= row; star++)
            System.out.print("*");
            System.out.println();
        }

}

}
Lion-O - 30 Oct 2006 01:58 GMT
> why isn't the second part of my triangle not working?

> for (int row = 1; row <= nLines; row++)
> {
>     for (int star= 1; star <= row; star++)
>             System.out.print("*");
>             System.out.println();
>         }

Note the missing {  ?

Signature

Groetjes, Peter

.\\ PGP/GPG key: http://www.catslair.org/pubkey.asc

Lew - 30 Oct 2006 02:24 GMT
>> why isn't the second part of my triangle not working?
>
[quoted text clipped - 6 lines]
>
> Note the missing {  ?

Actually, the braces matched.  The for loop has only one line, the
System.out.print().

Note that in the original post there were three opening braces and three
closing braces.  Adding one opening brace would cause compilation failure.

- Lew
Lew - 30 Oct 2006 02:34 GMT
> why isn't the second part of my triangle not working?
> import java.util.*;
[quoted text clipped - 15 lines]
>
> }

First of all, what do you mean by the "second part" of the loop, and in what
way is it not working?

Second of all, you really should use spaces to show indentation, not tabs, and
get things lined up in a way that makes sense.  In your post, it looks as if
you intend the println() call to appear in the inner loop.  That brings up

Third of all, you should enclose even one-line loop bodies in braces.
Combined with the aforementioned indentation fubar, it makes it extremely hard
to understand what you're trying to accomplish.

Please rephrase your question to indicate:
- What exactly you are trying to accomplish, and
- What exactly you are getting instead.

Oh, and you should name classes with an initial upper-case letter: "Triangle",
not "triangle".

Simply stating that "the second part of your triangle [is] not working"
doesn't give the rest of us enough information to help you.  We need the details.

For what it's worth, I ran your code and it worked just fine, yielding the
output:

*
**
***
****
*****

- Lew
Patricia Shanahan - 30 Oct 2006 03:09 GMT
> why isn't the second part of my triangle not working?
> import java.util.*;
[quoted text clipped - 15 lines]
>
> }

If you really mean "Why isn't it not working?" the answer is because the
loops are correct, assuming you wanted a triangle of asterisks, though
very untidy.

Patricia


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.