# Hourglass using recursion

Printable View

• 05-01-2012, 11:33 PM
nweid1
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:

Code:

* * * *
* * *
* *
*
* *
* * *
* * * *

and so far I got this far:

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.

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();
}
}

• 05-02-2012, 01:56 AM
doWhile
Re: Hourglass using recursion
Cross posted at Hourglass using recursion
• 05-02-2012, 04:15 AM
nweid1
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.
• 05-02-2012, 05:30 AM
DarrylBurke
Re: Hourglass using recursion
One more: Hourglass Using Recursion - Java | Dream.In.Code

Cross posters don't deserve help.

db
• 05-02-2012, 05:32 AM
nweid1
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
• 05-02-2012, 05:33 AM
DarrylBurke
Re: Hourglass using recursion
Quote:

Originally Posted by nweid1
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
• 05-02-2012, 05:37 AM
nweid1
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.
• 05-02-2012, 05:44 AM
DarrylBurke
Re: Hourglass using recursion
Quote:

Originally Posted by nweid1
/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
• 05-02-2012, 05:50 AM
doWhile
Re: Hourglass using recursion
Quote:

Originally Posted by nweid1
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.
• 05-02-2012, 07:07 AM
nweid1
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.
• 05-02-2012, 07:09 AM
nweid1
Re: Hourglass using recursion
Quote:

Originally Posted by doWhile
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.
• 05-02-2012, 07:57 AM
pbrockway2
Re: Hourglass using recursion
• 05-02-2012, 09:11 AM
DarrylBurke
Re: Hourglass using recursion
Quote:

Originally Posted by pbrockway2

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