Results 1 to 8 of 8

Thread: Loop Help

  1. #1
    Big Lewy is offline Member
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default 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 08:29 PM. Reason: added [code] ... [/code] tags

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default 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 ...
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default 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

  5. #5
    Big Lewy is offline Member
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default 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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Loop Help

    Thanks for repeating your requirements. You have already received 3 replies. Have you followed their advice?

  7. #7
    Big Lewy is offline Member
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default 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.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 7
    Last Post: 04-03-2013, 08:26 PM
  2. Converting a for loop to a do-while loop
    By awesom in forum New To Java
    Replies: 1
    Last Post: 11-23-2011, 03:02 PM
  3. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  4. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  5. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •