• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

MOOC Factorial help

 
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys.  I want to avoid using the internet to look up for a solution to solve a factorial by typing in a number after being asked for it, then have it calculate from 1*2*3 = 6 if I ask it to return a factorial for the number 3.   The calculation is supposed to come out as 6 for n=3.  However, for my code, the answer comes out as 4, not 6 for when n=3.  Can anyone give me a tip or help me think other way to get this done right without giving me an answer, please.  Appreciate it!

 
author & internet detective
Posts: 42056
926
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,
You are using i to mean two different things. Try to work out the factorial on paper. What information do you need to keep track of? Picture it as two columns.
 
Mike Skyriach
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Mike,
You are using i to mean two different things. Try to work out the factorial on paper. What information do you need to keep track of? Picture it as two columns.



Thanks for the quick reply!  I see..

I assume I have to declare another int variable.
1x2 = 2
2 x 3 = 6.

So I suppose I need to assign the next loop of the previous answer and multiply by the next incremental value.
 
Jeanne Boyarsky
author & internet detective
Posts: 42056
926
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Skyriach wrote:I assume I have to declare another int variable.
1x2 = 2
2 x 3 = 6.

So I suppose I need to assign the next loop of the previous answer and multiply by the next incremental value.


Yup!
 
Marshal
Posts: 80133
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And don't use Integer.parseInt when the Scanner object can do the parsing for you.
 
Mike Skyriach
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:And don't use Integer.parseInt when the Scanner object can do the parsing for you.



Really?  Why is the mooc.fi course teaching me to use Integer.parseInt and not Scanner parse method?  What's the difference between those two methods?
 
Campbell Ritchie
Marshal
Posts: 80133
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Skyriach wrote:. . . Why is the mooc.fi course teaching me to use Integer.parseInt and not Scanner parse method?

Good question. It is for them to justify why they are teaching that.

What's the difference between those two methods?

It isn't two methods; it is three. Integer.parseInt takes a String already provided, and Scanner.nextInt takes a file or the keyboard or similar and reads directly to an int. Note the names of the methods.You need some way to get the String into your program before you can parse it, so you need a preceding method call to get the text to parse.
The parseInt and nextInt methods are related to each other, but they throw different exceptions if your format is wrong. You can catch such exceptions, but you can avoid all Exceptions from the keyboard by combining nextInt and hasNextInt, as Rob Spoor taught me a long time ago.Lines 3 and 4 may go in either order.
[edit]That code will not throw exceptions as long as System.in remains open. It will not work reliably for reading files.
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic