Results 1 to 13 of 13
  1. #1
    nweid1 is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Hourglass using recursion

    I have to write a recursive program that makes an hourglass that suites the size of the user's input. For example, if the user types 4, this should print:

    Java Code:
    * * * *
     * * *
      * *
       *
      * *
     * * *
    * * * *
    and so far I got this far:

    Java Code:
    * * * *
    * * *
    * *
    *
    * *
    * * *
    * * * *
    so I just need to figure out how to get the spaces but I'm not sure how to do this. Thanks for the help.

    Java Code:
    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    public class Hourglass {
    
    	public static void main(String[] args) {
    
    		Scanner console = new Scanner(System.in);
    		System.out.println("Enter an integer greater than or equal to 1");
    		int n = 0;
    		try {
    			n = console.nextInt();
    		} catch (InputMismatchException e) {
    			e.getMessage();
    		}
    		if (n < 1) {
    			System.out.println("Invalid input");
    		} else {
    			System.out.println("");
    			hourglass(n, 0);
    			System.out.println("");
    		}
    
    	}
    
    	public static void hourglass(int n, int count) {
    		recursion(n, count);
    		if(n > 1) {
    			hourglass(n-1, count);
    			recursion(n, count);
    		}
    	}
    
    	public static void recursion(int n, int count) {
    		System.out.print("* ");
    		if (n > 1)
    			recursion(n - 1, count);
    		else
    			System.out.println();
    	}
    }
    Last edited by nweid1; 05-02-2012 at 12:41 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Hourglass using recursion

    Cross posted at Hourglass using recursion

  3. #3
    nweid1 is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Hourglass using recursion

    Hey, thanks for wasting your time and my time by posting "cross posted" but not helping with either of the posts.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Hourglass using recursion

    One more: Hourglass Using Recursion - Java | Dream.In.Code

    Cross posters don't deserve help.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    nweid1 is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Hourglass using recursion

    Cross posters don't deserve help? What are you, 5 years old? Grow up and find something better to do than patrolling programming forums lol

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Hourglass using recursion

    Quote Originally Posted by nweid1 View Post
    Cross posters don't deserve help? What are you, 5 years old? Grow up and find something better to do than patrolling programming forums lol
    Ah, the misbegotten sense of entitlement of the least worthy!

    /yawns

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    nweid1 is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Hourglass using recursion

    /yawns? Come on man you must still be living in your mom's basement. I'm still confused as to why you're on this post if you're just going to act like a kid.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Hourglass using recursion

    Quote Originally Posted by nweid1 View Post
    /yawns? Come on man you must still be living in your mom's basement. I'm still confused as to why you're on this post if you're just going to act like a kid.
    Carry on wasting time antagonizing the very people who might have helped you, all the way to your failed grade.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Hourglass using recursion

    Quote Originally Posted by nweid1 View Post
    Hey, thanks for wasting your time and my time by posting "cross posted" but not helping with either of the posts.
    I do recommend you read the links provided to you on other forums, in particular the following:
    The problems with cross-posting
    We do feel like our time is wasted, not for posting crosspost notices, but for replying to a post that has been answered and/or discussed elsewhere that is not disclosed.

    Cross posting is not against forum rules, but it has to do with respect. Presuming you are a reasonable person: you ask for help from unpaid volunteers - you should respect those that are trying to help. Otherwise, we all loose respect for you and your chances of receiving help - here or elsewhere now and in the future, go to 0.
    Last edited by doWhile; 05-02-2012 at 06:54 AM.

  10. #10
    nweid1 is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Hourglass using recursion

    The fact that you are limiting another person's knowledge on a subject because of the crime of cross-posting just makes you out to be a...jerk. Sorry if "jerk" seems kiddish but I wouldn't want to get reported to the programming police for saying something vulgar.

  11. #11
    nweid1 is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Hourglass using recursion

    Quote Originally Posted by doWhile View Post
    I do recommend you read the links provided to you on other forums, in particular the following:
    The problems with cross-posting
    We do feel like our time is wasted, not for posting crosspost notices, but for replying to a post that has been answered and/or discussed elsewhere that is not disclosed.

    Cross posting is not against forum rules, but it has to do with respect. Presuming you are a reasonable person: you ask for help from unpaid volunteers - you should respect those that are trying to help. Otherwise, we all loose respect for you and your chances of receiving help - here or elsewhere now and in the future, go to 0.
    Advice taken. From now on I'll take note to it but seeing as though I rarely use this forum I didn't know it was such a huge deal to widen my range of advisers. But I must say though that yet again I have a comment and yet no help. Thank you.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Hourglass using recursion

    Thanks, I've looked after those two and I'm keeping an eye on two other forums that haven't so far figured in the list.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Recursion.
    By Cruncher in forum Advanced Java
    Replies: 30
    Last Post: 05-10-2010, 02:06 PM
  2. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:26 PM
  3. Need to change cursor to hourglass at server side
    By bgupta in forum Java Servlet
    Replies: 0
    Last Post: 02-16-2009, 08:35 AM
  4. Recursion
    By jachandru in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 01:52 PM
  5. Recursion help
    By rjg_2186 in forum New To Java
    Replies: 1
    Last Post: 01-02-2009, 09:03 AM

Posting Permissions

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