-
Help with loops
I have two questions how can I change the following code so it uses
a) while loops rather than do loops
b) using for loops
Code:
public class StarMethod3
{
private void stars(int numstars)
{
int line = 4;
do
{
int stcount = numstars;
do
{
System.out.print("*");
stcount--;
}while (stcount > 0);
System.out.println();
line--;
}while (line > 0);
}
public static void main(String args[])
{
StarMethod3 s = new StarMethod3();
s.stars(2);
}
}
-
Re: Help with loops
-
Re: Help with loops
I've just tried re arranging the code I know that it should be like:
while (true){
// your code goes here
}
in the above format but I get errors?
-
Re: Help with loops
-
Re: Help with loops
I'm not sure how to change do while to while loop so i tried:
Code:
public class StarMethod3
{
private void stars(int numstars)
{
int line = 4;
while (int stcount = numstars);
{
System.out.print("*");
stcount--;
}
while (stcount > 0);
System.out.println();
line--;
}
while (line > 0);
}
public static void main(String args[])
{
StarMethod3 s = new StarMethod3();
s.stars(2);
}
}
It wont compile, do I put the arguments in one line?
-
Re: Help with loops
I managed to do it using
Code:
public class StarMethod3
{
public void stars()
{
int line = 3;
while (line > 0)
{
int stcount = 3;
while (stcount > 0)
{
System.out.print("*");
stcount--;
}
System.out.println();
line--;
}
}
public static void main(String args[])
{
StarMethod3 s = new StarMethod3();
s.stars();
}
}
i just need help coverting using only for loop
-
Re: Help with loops
ive now managed to do with for loops using
Code:
//using for loops
public class StarMethod3
{
private void print()
{
for (int count=4;count>0;count--)
System.out.print("*");
System.out.println();
}
public static void main(String args[])
{
StarMethod3 s = new StarMethod3();
s.print();
}
}
this prints out four stars how do i include 'line' so it prints out multiple lines?
-
Re: Help with loops
You have to slow down and really think about what you're trying to do. You can't just rearrange code and hope for the best. What are you trying to achieve? Break your problem down into smaller steps, and take them one step at a time.