Re: URgh, drawing a square
If you want help, you\'ll have to provide an SSCCE that demonstrates the problem. You\'re close, but what is BIO? I assume it\'s a Scanner?
Also, I\'d recommend stepping through this with a debugger, or at least adding print lines that let you know what\'s going on. For example, what is the value of sizeofsquare each iteration through the loop? When will it be > 40 to allow the loop to exit?
Re: URgh, drawing a square
Code java:
public class Main
{
public static void main(String args[])
{
System.out.print("#Number Of Stars: ");
int sizeofsquare = 5;
int down = 1;
int across = 2;
System.out.println();
while ( down <= sizeofsquare ){
System.out.print("*");
while ( across <= sizeofsquare ){
System.out.print("*");
across = across + 1;
}
System.out.println();
down = down + 1;
}
}
}
Right i\'ve managed to shrink it abit but at the moment i get:
*****
*
*
*
*
when i need:
*****
*****
*****
*****
*****
I don\'t really understand why the while loop containing the across * stops either :S
Re: URgh, drawing a square
Again, I\'d recommend stepping through this with a debugger or at least adding print lines to understand what\'s going on. Pay special attention to the value of across each time through the loops.
Re: URgh, drawing a square
I used the debugger and from what it showed me the while across loop is doing its job then thinking its done and letting the other loop then do its job.
However ive tried to move the across=across + 1 and it just makes the across while loop continue forever.
Do i need to add another while loop to the mix?
Re: URgh, drawing a square
Quote:
Originally Posted by
BITmixit
I used the debugger and from what it showed me the while across loop is doing its job then thinking its done and letting the other loop then do its job.
I'm not sure you understand how the loops are working. One is inside the other, they aren't happening one after another.
Quote:
Originally Posted by
BITmixit
However ive tried to move the across=across + 1 and it just makes the across while loop continue forever.
What did you expect that to accomplish?
Quote:
Originally Posted by
BITmixit
Do i need to add another while loop to the mix?
What do you expect that to accomplish?
I suggest you slow down and take a closer look at what your existing program is doing before you attempt to add anything to it.
Another approach would be to write a method that prints a single row of a certain parameterized length, then call that method the correct number of times.
Also, why are you using while loops instead of for loops?
Re: URgh, drawing a square
Fixed it and its working now, took a closer look then sort of had a duh moment and realised i could reset the across variable outside of the across loop thus resetting it till the down loop was done.
Have submitted it to the uni server and its correct :D