• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with a simple for loop  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, all!

I'm trying to learn Java and my current project is to write a short program to determine the factorial of a number entered by the user. I haven't looked. There may be a method that will do it, but I want to use a for loop specifically.

What I have compiles just fine. I'm actually pretty thrilled just with that. But I would like the thing to do what I want it to do! I think I'm probably overlooking something quite simple, but any help would be greatly appreciated.

Here is what I have:

 
Sheriff
Posts: 4298
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is probably in this line:

product = product * (i+=1) ;

Is that doing what you want it to do? What is "i" at this point?
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ez,
Welcome to CodeRanch! I edited your post to add code tags to make it easier to read.

Here's a hint:
You increment i twice - once in the line declaring the for loop and once inside it. Which of those do you think you don't want?

Remember there is a difference between += (adds and assigns) and + (just adds and leaves the variable alone)
 
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to the good hints you've already been given, pay close attention the initial value of i and what its value will be when your condition will terminate the loop. Ensure that you use only the values 1-n. As a matter of fact, you could "maximize efficiency" and start at 2.
 
Ez Burdett
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it! I'll be able to sleep tonight after all.

int i = 1

Thanks for the help guys! I especially appreciate that y'all gave me hints instead of solutions.

And sorry for the formatting issues. I knew it might be an issue but I was tired of figuring stuff out for a bit.

Thanks again!
 
Ez Burdett
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The hint about "maximizing efficiency" was good too. Right now, I'm more interested in getting stuff to work than in subtle stuff like that. BUT I am trying to start thinking like that already, so thanks!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ez Burdett wrote:Thanks for the help guys! I especially appreciate that y'all gave me hints instead of solutions.bit.

It's the best way to learn!

Ez Burdett wrote:And sorry for the formatting issues.

No worries. You are new to the site and aren't expected to know. (Incase you are wondering, what I did was highlight the code and then click the "code" button above the post.)
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ez Burdett wrote: . . . I'm more interested in getting stuff to work than in subtle stuff like that. . . .
There is a great advantage in making your code nice and simple. Not only is it easy to read, but the JVM can optimise such code for getter performance much more easily than subtle code. More info here.

And welcome again
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are good reasons for the conventional forms of the for loop, e.g.Not only is it familiar, but also, millions of people have written loops like that and found they work correctly in many situations.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!