how to write using recursive method for print out example number 123456789987654321. and make 9 as the begin number
recursion
Page 1 of 111 Replies - 1297 Views - Last Post: 31 August 2009 - 02:29 AM
Replies To: recursion
#2
Re: recursion
Posted 28 August 2009 - 11:32 PM
public class recursion
{
private final int max = 9; // Constant
private static boolean flag = false;
void myMethod( int counter)
{
if(flag)
{
System.out.print(counter);
if (counter == 1)
return;
myMethod(--counter);
}
else
{
System.out.print(counter);
if (counter == max)
{
flag = true;
myMethod(counter);
return;
}
myMethod(++counter);
}
}
public static void main(String[] args)
{
new recursion().myMethod(1);
}
}
Obviously you can spice it up. This was just a quick hack.
This post has been edited by trixt.er: 28 August 2009 - 11:43 PM
#3
Re: recursion
Posted 28 August 2009 - 11:38 PM
Is this a double post with different accounts or just two people with the same assignment, both ignoring the rules?
And trixter, your making this more complicated than it needs to be.
And trixter, your making this more complicated than it needs to be.
This post has been edited by syfran: 28 August 2009 - 11:40 PM
#4
Re: recursion
Posted 28 August 2009 - 11:44 PM
#5
Re: recursion
Posted 28 August 2009 - 11:53 PM
public class recursion
{
private static boolean flag = false;
void myMethod(int counter)
{
System.out.print(counter);
if (counter == 9 || flag)
{
if (counter == 1) // Base case.
return;
flag = true;
myMethod(--counter);
}
else
myMethod(++counter);
}
public static void main(String[] args)
{
new recursion().myMethod(1);
}
}
#6
Re: recursion
Posted 29 August 2009 - 12:00 AM
public class recursion
{
private static boolean flag = true;
void myMethod(int counter)
{
System.out.print(counter);
if (counter != 9 && flag)
myMethod(++counter);
else if (counter != 1)
{
flag = false;
myMethod(--counter);
}
}
public static void main(String[] args)
{
new recursion().myMethod(1);
}
}
That was fun!
#7
Re: recursion
Posted 29 August 2009 - 12:00 AM
Trixter, the goal here isn't to hand out code. It's to help people to learn programming, you can't do that unless you try.
#8
Re: recursion
Posted 30 August 2009 - 06:51 PM
#9
Re: recursion
Posted 31 August 2009 - 02:18 AM
Write a recursive method that could print out the numbers 123456789987654321. Go to the next line after each displayed number. Assume that number 9 is a begin number. In your program, identify the base and recursive case.
#10
Re: recursion
Posted 31 August 2009 - 02:21 AM
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and other members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
Please post like this:
Thank you for helping us help you.
Please post like this:
Thank you for helping us help you.
This post has been edited by Travis1012: 31 August 2009 - 02:22 AM
#11
Re: recursion
Posted 31 August 2009 - 02:26 AM
Please don't tell me this is the same guy posting for the third time...
#12
Re: recursion
Posted 31 August 2009 - 02:29 AM
Page 1 of 1

New Topic/Question
Reply



MultiQuote





|