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:
* * * *
* * *
* *
*
* *
* * *
* * * *
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();
}
}
Re: Hourglass using recursion
Hello nWeid1!
I can't see the difference between the output as it should be and the one you actually get. What do you mean by "figure out how to get the spaces"?
Re: Hourglass using recursion
I just updated it. for some reason it won't display the spaces unless its
Re: Hourglass using recursion
Where in your code do you have these spaces? I can only see two print statements in the recursion code -one for stars and the other for an empty line.
Re: Hourglass using recursion
Well, I don't. I don't know where to implement the code to have them print out properly which is my problem.
Re: Hourglass using recursion
For this kind of problems you should write down the logic of your program and *test* it in a piece of paper before you implement code.
Also, what is the variable count? Did you think of a way to use it to accomplish what you want?
Re: Hourglass using recursion
Yeah I figured I could use count so that every line of stars there should add 1 space before the next line of stars. I also thought of creating a separate method just for the spaces but I don't know if that will work out.
Re: Hourglass using recursion
Quote:
Originally Posted by
nWeid1
Yeah I figured I could use count so that every line of stars there should add 1 space before the next line of stars. I also thought of creating a separate method just for the spaces but I don't know if that will work out.
You should try it and see what is happening. But again I suggest you writing down the logicof the program.
Hint: not sure if you are allowed to use them, but for loops is a very common approach in such kind of problems.
Re: Hourglass using recursion
Ok I'll try to write it down. And yeah my teacher said we can't use any for loops, hence the reason I'm using recursion in the first place
Re: Hourglass using recursion
Re: Hourglass using recursion
One more: Hourglass Using Recursion - Java | Dream.In.Code
db
edit The OP is presently throwing a temper tantrum on the thread linked by copeg