Results 1 to 8 of 8
Thread: Help with loops
- 09-18-2013, 07:02 PM #1
Member
- Join Date
- Sep 2013
- Posts
- 26
- Rep Power
- 0
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
Java 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); } }
- 09-18-2013, 07:06 PM #2
Re: Help with loops
What have you tried?
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 09-18-2013, 07:10 PM #3
Member
- Join Date
- Sep 2013
- Posts
- 26
- Rep Power
- 0
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?
- 09-18-2013, 07:11 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Help with loops
What errors?
Please do not ask for code as refusal often offends.
** This space for rent **
- 09-18-2013, 07:15 PM #5
Member
- Join Date
- Sep 2013
- Posts
- 26
- Rep Power
- 0
Re: Help with loops
I'm not sure how to change do while to while loop so i tried:
Java 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); } }
- 09-18-2013, 07:29 PM #6
Member
- Join Date
- Sep 2013
- Posts
- 26
- Rep Power
- 0
Re: Help with loops
I managed to do it using
Java 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(); } }
- 09-18-2013, 07:51 PM #7
Member
- Join Date
- Sep 2013
- Posts
- 26
- Rep Power
- 0
Re: Help with loops
ive now managed to do with for loops using
Java 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(); } }
- 09-18-2013, 08:04 PM #8
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.
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
Similar Threads
-
Help with loops!
By jrelvi23 in forum New To JavaReplies: 9Last Post: 09-04-2012, 02:36 PM -
[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03-22-2011, 05:02 AM -
loops
By curioustoknow in forum New To JavaReplies: 3Last Post: 02-06-2011, 02:45 PM -
need some help with loops!
By Chewart in forum New To JavaReplies: 2Last Post: 12-04-2009, 12:32 AM -
these loops...
By Blaedel in forum New To JavaReplies: 0Last Post: 10-01-2009, 07:59 PM
Bookmarks