Results 1 to 4 of 4
  1. #1
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Post Loop inside a switch

    Just a simple question :
    If you have a switch statement that tests a charand whatever letter it is how do you make a loop so that it prints out lets say 30 times?
    can you do it without using something else like:
    while this is less that 31
    print out this

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not sure I get exactly what you're trying to do here. If you don't get a decent answer by others, you may want to expand on your explanation a bit and even post a code attempt at solving it.

    Much luck!

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Why would you even need a switch?
    Java Code:
    public static void printchar(char c) {
       for(int i = 0; i < 30; i++)
          System.out.println(c);
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, why are embedded a loop in switch. It's not a good design at all, if someone else read your code will confuse, and difficult to understand.

    As m00nchile suggest in the above post, do that looping stuff in a different method, and call the appropriate method in case statement. There is no limitation, you can use many number of methods within your classes.

Similar Threads

  1. Problem printing inside FOR loop
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 06:02 PM
  2. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 02:45 AM
  3. println doesn't print from inside for loop, et.al.
    By rdtindsm in forum New To Java
    Replies: 5
    Last Post: 03-27-2009, 02:19 PM
  4. switch
    By dj kourampies in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 09:46 AM
  5. Switch help please!!!!
    By soc86 in forum New To Java
    Replies: 6
    Last Post: 11-23-2008, 08:25 PM

Posting Permissions

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