Milo Wuertz

Greenhorn

Posts: 2

posted 4 years ago

Alrighty, I'm new in java and I'm having a bit of an issue getting this loop to do what I want. I've tried organizing them in just about every way, with different results, but not what it's supposed to be. It's supposed to be a factorial finding application.

I'm working in Netbeans. I want it to display the answers and keep asking for another number to find the factorial for as long as the user inputs a valid number. If the number entered is a negative number, I want it to say it can't find the factorial and to enter another number. If the number entered is zero, I want the answer to be given, and then the application terminated. Right now, if I enter a number it will find the factorial and ask for another number but if I enter another number it will think I entered a 0.

Enter a number to find the factorial:5

5

20

60

120

120

Factorial of 5 is 120

Enter another number or 0 to quit:

5

The factorial of 0 is 1. Terminating application.

(also, don't know if the extra 120 is necessary, and if I could cut that out that would be great!)

If I enter a 0, it will still ask for another number and then say number I entered was zero.

Enter a number to find the factorial:0

Factorial of 0 is 1

Enter another number or 0 to quit:

5

The factorial of 0 is 1. Terminating application.

Then when I enter a negative number, the second number will ultimately give me what I need on the first number.

run:

Enter a number to find the factorial:-9

Factorial of -9 is 1

Enter another number or 0 to quit:

5

Cannot compute. Enter a number greater than 0.

I have been mulling over this all week, any help would be appreciated!!

I'm working in Netbeans. I want it to display the answers and keep asking for another number to find the factorial for as long as the user inputs a valid number. If the number entered is a negative number, I want it to say it can't find the factorial and to enter another number. If the number entered is zero, I want the answer to be given, and then the application terminated. Right now, if I enter a number it will find the factorial and ask for another number but if I enter another number it will think I entered a 0.

Enter a number to find the factorial:5

5

20

60

120

120

Factorial of 5 is 120

Enter another number or 0 to quit:

5

The factorial of 0 is 1. Terminating application.

(also, don't know if the extra 120 is necessary, and if I could cut that out that would be great!)

If I enter a 0, it will still ask for another number and then say number I entered was zero.

Enter a number to find the factorial:0

Factorial of 0 is 1

Enter another number or 0 to quit:

5

The factorial of 0 is 1. Terminating application.

Then when I enter a negative number, the second number will ultimately give me what I need on the first number.

run:

Enter a number to find the factorial:-9

Factorial of -9 is 1

Enter another number or 0 to quit:

5

Cannot compute. Enter a number greater than 0.

I have been mulling over this all week, any help would be appreciated!!

Milo Wuertz

Greenhorn

Posts: 2

posted 4 years ago

Thank you for the code, it gave me a bit of an idea, but when I run the application it starts by asking for a number and also saying the number was zero and to enter another number. Is it possible at all to create something like this using a simple do...while, or while? (not sure we're supposed to be using the exitAlllLoop yet? for a class.)

I've started with just the basic loop.. and I'm having trouble getting just the basic loop to loop correctly.

It will find the factorial but when it asks for another number it will not repeat the loop.

run:

Enter a number to find the factorial:5

5

20

60

120

120

Factorial of 5 is 120

Enter another number or 0 to quit:

5

BUILD SUCCESSFUL (total time: 3 seconds)

Maybe I'm just tired, but I can't figure out what to do to make it work. I've tried looking at other examples and the book but alas, no luck.

I've started with just the basic loop.. and I'm having trouble getting just the basic loop to loop correctly.

It will find the factorial but when it asks for another number it will not repeat the loop.

run:

Enter a number to find the factorial:5

5

20

60

120

120

Factorial of 5 is 120

Enter another number or 0 to quit:

5

BUILD SUCCESSFUL (total time: 3 seconds)

Maybe I'm just tired, but I can't figure out what to do to make it work. I've tried looking at other examples and the book but alas, no luck.

posted 4 years ago

You are quite near to the solution. As Winston rightly says, "Forget coding for now, Take a pencil and paper and write what the program is supposed to do", and that's what you have to do now. You have rightfully figured out the problem that the existing code does not repeat itself. Now you have to think of two issues;

1. After the first factorial is computed, the program code is supposed to prompt

the user for a choice to continue or not. If the choice is Yes, then the block of

code that accepts the user input should repeat again. So your clue here is to

figure out how to make the code loop and ask for another input.

2. Remember to reassign the variable factorial to a default value because if you

do not then with the next user input, the variable factorial will start its computation

from the previous value stored in it. Example:

Lets say when the

program first executed, the user entered 3 so the factorial was computed to

3x2x1=6, Now if the user wants to calculate another factorial and you do not

assign it a default value then previous factorial value will be computed for the

current cycle. So if variable factorial is not assigned a default value and user

enters 5 then the new factorial will be 6x5x4x3x2x1=720

Hope this helps.

1. After the first factorial is computed, the program code is supposed to prompt

the user for a choice to continue or not. If the choice is Yes, then the block of

code that accepts the user input should repeat again. So your clue here is to

figure out how to make the code loop and ask for another input.

2. Remember to reassign the variable factorial to a default value because if you

do not then with the next user input, the variable factorial will start its computation

from the previous value stored in it. Example:

Lets say when the

program first executed, the user entered 3 so the factorial was computed to

3x2x1=6, Now if the user wants to calculate another factorial and you do not

assign it a default value then previous factorial value will be computed for the

current cycle. So if variable factorial is not assigned a default value and user

enters 5 then the new factorial will be 6x5x4x3x2x1=720

Hope this helps.

Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.

My Blog on Learning Analytics

posted 4 years ago

Milo,

Please DontWriteLongLines. It makes your thread very hard to read,

and it's actually bad coding practice.

I've broken yours up this time, but for future reference, please remember:

(the SSCCE page actually recommends 62)

And that includes string literals

Oh, and @Ashish: I removed the code blocks from your post as well, for the same reason.

Thanks.

Winston

Milo Wuertz wrote:Alrighty, I'm new in java...

Milo,

Please DontWriteLongLines. It makes your thread very hard to read,

and it's actually bad coding practice.

I've broken yours up this time, but for future reference, please remember:

*80 characters max*.

(the SSCCE page actually recommends 62)

And that includes string literals

*AND*comments.

Oh, and @Ashish: I removed the code blocks from your post as well, for the same reason.

Thanks.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Consider Paul's rocket mass heater. |