• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Help with Even Recursion Loop With User Input To Quit or Continue

 
Greenhorn
Posts: 27
Android C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm new to programming, I need to calculate the even number with recursion loop. I know the logic for it work but I can't make the user input loop with this program.
Thank you in advance!

 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What does the code do now, and what should it be doing instead?
 
Rancher
Posts: 662
10
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your do-while loop syntax is wrong. The correct should be like this
 
ShinRyu Kazuto
Greenhorn
Posts: 27
Android C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the list of things that I need to meet all the criteria:
Include a recursive method separate from the main method that will add together all of the even numbers between and including 1 and the value the user supplies.  For instance, if the user enters 10 then the recursive method will return 2 + 4 + 6 + 8 + 10 = 30.  If the user enters 5 then the recursive method will return 2 + 4 = 6.


In the main method:

   -Ask the user for an integer.  Call the recursive method, passing it the user input.
   -Include error handling for the input (the input must be an integer).
   -Display the original value and the result of the recursive method calculation.  Include an appropriate output message.
   -Repeat the program until the user chooses to quit.

so far I did this one above and it work but I still stuck with repeating the program until the user chooses to quit.
 
Randy Tong
Rancher
Posts: 662
10
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest you move the do-while loop to main method, so it can repeat the program  once factorial number is returned.




 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

1. First I'd suggest to remove all these comments. At least for now. If the exercise requires those, you can add them later, but they certainly don't help at the moment.

2. Please fix your indentation and formatting, that also doesn't help.

3. I see you have a variable of type char to which you are trying to assign value which is of type boolean (true/false). That's illegal in Java.

Do I understand you correctly, that you are trying to incorporate factorial function call to a loop? Doesn't this question give you a hint?

Pseudo code:

In order to achieve such construct, much better is to separate factorial calculation (or whatever that is) from asking user whether he/she would like to try again. So basically you need an extra method for that.

Once again
 
ShinRyu Kazuto
Greenhorn
Posts: 27
Android C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about this one? I did some changes but still won't work properly


 
Randy Tong
Rancher
Posts: 662
10
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use do-while loop better. From your latest code , while loop will never executed since you inputFlag is always set to false.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randy Tong wrote:From your latest code , while loop will never executed since you inputFlag is always set to false.


That is actually opposite, since OP made a common mistake:

To avoid that, much better would have been to write:
while (inputFlag) or while (!inputFlag)
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a bit confused by your code; in some places you mentioned factorials. Your factorial formula is, I am afraid, incorrect; I think you will get 0! = 0.
Don't talk about recursion and loops. It is recursion OR it is a loop. Actually, in many cases, a loop and a recursive solution are equivalent to each other.
This is how I would write a factorial program. This is a very simple and naïve program, it ignores the fact that there is no such thing as a factorial of a negative number. It also forgets that you will get an overflow error for an argument > 12 (I think you can calculate 21! with a long).
Also note the use of ?: instead of an if‑else
Another thing: I am using a simpler base case than you did: plain simple 0.That might not give you the sum of 0+2+4+6+8+10, but it does give you a good idea what a simple recursive method looks like.

And . . . welcome to the Ranch
reply
    Bookmark Topic Watch Topic
  • New Topic