Trying to count....

Hi, I am trying to create a method that will print arrows to the screen using a recursive solution. It will ask the user for a height, and the width will be the height/2 rounded up. It should also only receive the one parameter (height), and it will return an int value. The int value will be the total count of the arrows.

Here are two examples output:

height = 6

>

> >

> > >

> > >

> >

>

Total = 12

height = 7

>

> >

> > >

> > > >

> > >

> >

>

Total = 16

The part I am having trouble is getting a return value, that has all the arrows counted up. I don't want to use an equation as I already have (too cheap!!, I'm certain that's unacceptable, especially there is no need for it to be returned just printed out"). I know of a method where you have a variable increment every time an arrow is printed to the screen, but I don't see how I could do this recursively.

Here is my code so far:

publicclass test2{

publicstaticvoid main(String args[]){

System.out.print("\tPlease enter the height of your arrows: ");

int height = UserInput.readInt();

arrow(height/2);

if (height % 2 == 1){

int width = height/2 + 1;

printMid(width);

}

arrowBH(height/2);

double arrows;

arrows = (double) (.25*height * height + .5*height);

System.out.println("\tThe total number of arrows printed are: " + Math.round(arrows));

}

publicstaticvoid arrow(int h)

{

// Base case

if( h == 0 )return;

// Rec case

arrow( h-1 );// Rec step #1

printRow(h);// Rec step #2

}

publicstaticvoid arrowBH(int h )

{

// Base case

if( h == 0 )return;

// Rec case

printRow(h);// Rec step #2

arrowBH( h - 1);// Rec step #1

}

publicstaticvoid printMid(int width){

if (width == 0)

System.out.println();

else{

System.out.print("> ");

printMid(width - 1);

}

}

publicstaticvoid printRow(int n){

if( n == 0 )// Base case

System.out.println();

else// Rec case

{System.out.print("> ");

printRow( n-1);

}

}

}

[4374 byte] By [nndhawana] at [2007-11-27 0:40:58]
# 1

As you have only one class, why don't you use a field variable.

You increment this variable (counter) where you get an arrow.

int counter = 0;

System.out.println(">");

counter++;

System.out.println(">");

counter++;

System.out.println(">");

counter++;

System.out.println("no. of arrows=" + counter); //3

rym82a at 2007-7-11 22:54:11 > top of Java-index,Java Essentials,New To Java...
# 2
The method works, but I am trying to use the return type int from the arrow, arrowBH, printRow, and/or printMid methods in order to display how many arrows have been printed out.
nndhawana at 2007-7-11 22:54:11 > top of Java-index,Java Essentials,New To Java...
# 3

Similar

public static int countStar(int i) {

if (i == 0) {

return 0;

} else {

for (int j=0; j<i; j++) {

System.out.print(">");

}

System.out.println();

return (i + countStar(--i));

}

}

public static void main(String[] args) {

System.out.println("total=" + countStar(5));

}

rym82a at 2007-7-11 22:54:11 > top of Java-index,Java Essentials,New To Java...
# 4

> The method works, but I am trying to use the return

> type int from the arrow, arrowBH, printRow, and/or

> printMid methods in order to display how many arrows

> have been printed out.

If h is the height of your arrow structure, then the number of arrows can be calculated by this formula:int arrows = (int)Math.floor(((h+1)*(h+1))/4.0);

Good luck.

prometheuzza at 2007-7-11 22:54:11 > top of Java-index,Java Essentials,New To Java...