Re: Need help with loop
- From: Lew <lew@xxxxxxxxxxx>
- Date: Sun, 29 Oct 2006 20:34:26 -0500
meena.desani wrote:
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();
}
}
}
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
.
- References:
- Need help with loop
- From: meena.desani
- Need help with loop
- Prev by Date: Re: Need help with loop
- Next by Date: Re: Need help with loop
- Previous by thread: Re: Need help with loop
- Next by thread: Re: Need help with loop
- Index(es):