 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
meena.desani Guest
|
Posted: Mon Oct 30, 2006 6:34 am Post subject: Need help with loop |
|
|
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();
}
}
} |
|
| Back to top |
|
 |
Lion-O Guest
|
Posted: Mon Oct 30, 2006 6:58 am Post subject: Re: Need help with loop |
|
|
| Quote: | why isn't the second part of my triangle not working?
|
| Quote: | for (int row = 1; row <= nLines; row++)
{
for (int star= 1; star <= row; star++)
System.out.print("*");
System.out.println();
}
|
Note the missing { ?
--
Groetjes, Peter
..\\ PGP/GPG key: http://www.catslair.org/pubkey.asc |
|
| Back to top |
|
 |
Lew Guest
|
Posted: Mon Oct 30, 2006 7:24 am Post subject: Re: Need help with loop |
|
|
Lion-O wrote:
| Quote: | 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 { ?
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 |
|
| Back to top |
|
 |
Lew Guest
|
Posted: Mon Oct 30, 2006 7:34 am Post subject: Re: Need help with loop |
|
|
meena.desani wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Patricia Shanahan Guest
|
Posted: Mon Oct 30, 2006 8:09 am Post subject: Re: Need help with loop |
|
|
meena.desani wrote:
| Quote: | 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();
}
}
}
|
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 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|