...

View Full Version : Need help with using nested loops!



ventil
05-07-2009, 04:14 AM
Hello, I am trying to make an application that displays a pattern of asterisks a certain way. the only output statements I could use are to get the pattern below. My question would be which way to use the nested loop, (do i do while(if else)...

System.out.print( "*" );
System.out.println();
System.out.print( " " );

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


public class Triangles
{
public void drawTriangles()
{
int row;

int column;

int space;

while ( row <= 1 )
{

System.out.print( "*" );

row++;

}
}

}

adios
05-07-2009, 04:35 AM
Moved to Java forum.

Oh, yeah. I'm not a mod. Hopefully one will. :)

Philip M
05-07-2009, 11:53 AM
ventil - This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Ask a mod to move this over.

ventil
05-07-2009, 02:03 PM
Yea I am sorry i will.

Sphynx
05-07-2009, 02:52 PM
I don't truly understand the question, but if I wanted to create that triangle with asterix's using a nested loop it'd look like this:


maxX = 10;

for( x = 0; x < maxX; x++ ) {
for( y = 0; y < x; y++ ) print( "*" );
print( "<br>" );
}

ventil
05-07-2009, 03:08 PM
The question is confusing sorry, the problem with that is I cant set a value to any of the variables, I was also wondering if an array would work in this situation,

Sphynx
05-07-2009, 03:18 PM
int x; while( x < 11 ) { int y; while( y < x ) print( "*" ); }?

ventil
05-07-2009, 03:40 PM
Ok this is a homework assignment, but I dont want anyone to do it for me, all i want is a hint on how to start it.

the triangle should look like this :

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

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



public class Triangles
{
public void drawTriangles()
{
int row;

int column;

int space;

//first triangle
/* Write code to display the first triangle.
Use nested for loops.
The outer loop should control which row of
asterisks is being displayed. The inner loop
should display one asterisk at a time. */

//Second triangle
/*Write code to display the third triangle using
techniques similar to the first triangle*/



}

}


I have no idea on how to start the for loop, because the compiler gives me an error saying that the variable hasnt been initialized which is true when i dont give it a value, I guess i dont really understand how the application is supposed to work but the template doesnt show any of the variables having a value, is there another way that can be done without setting any values?

ventil
05-07-2009, 03:41 PM
and the only output statements i can use are

System.out.print( "*" );
System.out.println();
System.out.print( " " )

Sphynx
05-07-2009, 03:50 PM
The first loop I gave you will work in that class and do as you wish (so will the 2nd one, I just forgot the <br> (or println()) in there. :)

Just replace the x with row, the y with column, the print with your System.out.print, the print( "<br>" ) with the println(). Just play with it some. ;)

ventil
05-07-2009, 04:03 PM
well i tried that but like i said because the variable arent declared the compiler is giving me an error saying that variable have not been initialized



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum