* * * * * * * * * * * * * * * * * * *
So far I am able to print the stars but I'm not sure how to deal with the spaces. My teacher said to use a count and I added it but I'm not sure how to get use it appropriately. Here is an example of what prints
* * * * * * * * * * * * * * * * * * *
and here is my 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);
if(n > 1) {
spaces(n, count);
hourglass(n-1, count);
recursion(n);
}
}
public static void recursion(int n) {
System.out.print("* ");
if (n > 1)
recursion(n - 1);
else
System.out.println();
}
public static void spaces(int count, int n) {
if(count<n) {
spaces(count-1, n);
}
System.out.print(" ");
}
}
Thanks!
This post has been edited by baavgai: 30 April 2012 - 04:27 AM
Reason for edit:: tagged stars, because I couldn't tell the diff

New Topic/Question
Reply



MultiQuote




|