Results 1 to 8 of 8
Thread: Loop Help
- 09-18-2013, 09:22 PM #1
Member
- Join Date
- Sep 2013
- Posts
- 3
- Rep Power
- 0
Loop Help
Hi,
I was just wondering if I could make this loop any shorter. I don't know if there was a way to put all three loops into one and still get the same output.
Java Code:import java.util.Scanner; public class PrintA { public static void main(String[] args) { Scanner input = new Scanner(System.in); int count; int red; int blue; int green; red = input.nextInt(); count = 0; while ( count < red) { System.out.print ('A'); count++; } blue = input.nextInt(); count = 0; while ( count < blue) { System.out.print ('A'); count++; } green = input.nextInt(); count = 0; while ( count < green) { System.out.print ('A'); count++; } } }
Last edited by JosAH; 09-18-2013 at 09:29 PM. Reason: added [code] ... [/code] tags
- 09-18-2013, 09:30 PM #2
Re: Loop Help
You're doing the same thing 3 times in a row. Can you think of a way to do something multiples times without repeating code?
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, 09:31 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Loop Help
You could create a small method for the loops? The method takes a parameter telling how many times it should loop and print an 'A' ...
kind regards,
Jos
edit: darn, too slow again ...Build a wall around Donald Trump; I'll pay for it.
- 09-18-2013, 09:31 PM #4
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Loop Help
Well forget about the code then - try to explain it in English. What do you want to achieve functionally? Seems to me you want to print a certain number of A's. How many exactly?
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 09-19-2013, 01:05 AM #5
Member
- Join Date
- Sep 2013
- Posts
- 3
- Rep Power
- 0
Re: Loop Help
I enter a number for the color and then it prints the number of A's I entered. It is for a class I am taking. I just want my code to be more efficient.
- 09-19-2013, 03:51 AM #6
Re: Loop Help
Thanks for repeating your requirements. You have already received 3 replies. Have you followed their advice?
- 09-19-2013, 08:35 PM #7
Member
- Join Date
- Sep 2013
- Posts
- 3
- Rep Power
- 0
Re: Loop Help
thanks for the response,
I don't know very much about programming just yet. I'm in the fourth week of a college course called Introduction to Java. My professor asked me to write a code where it asks for a number for red blue and green and then print that number of a's for each color. I succeeded but I wanted to know if I could combine the while's and still get the same output.
- 09-19-2013, 08:41 PM #8
Re: Loop Help
With programming, there are a million different ways to do any one thing, so it's a little hard to answer general "how can I do this" type questions. You can probably combine the loops to accomplish this goal, but that might not jive well with the next leg of the assignment. You've been given several hints and suggestions, which you seem to be ignoring, so it's a bit hard to be more specific than we've been. If you're confused about something we said, take a stab at it and ask us a specific question.
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
-
When you have a choice between using either a for loop or a while loop...
By psx2514 in forum New To JavaReplies: 7Last Post: 04-03-2013, 09:26 PM -
Converting a for loop to a do-while loop
By awesom in forum New To JavaReplies: 1Last Post: 11-23-2011, 04:02 PM -
Problem with while loop, assigning a variable with a different value every loop? Help
By JavaProg in forum New To JavaReplies: 2Last Post: 11-07-2011, 03:25 AM -
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 03:12 AM -
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 06:46 PM
Bookmarks