03-22-2004, 09:14 PM | #1 (permalink) |
Crazy
Location: Long Island, NY
|
[java] need help w/small program
I need to write a program that allows a user to input a number and then output the factorial. (eg. user inputs 5 and program outputs 5!=5*4*3*2*1=120) then the program has to repeat so the user can continue to enter numbers until the user enters -999. This will stop the program.
Any help would be appreciated... I'm really stuck on this one. I don't really have any experience with java.
__________________
"Can't help it if I space in a daze, my eyes tune out the other way... I may switch off and go in a daydream... in this head my thoughts are deep, Sometimes I can't even speak, would someone be and not pretend, I'm off again in my world" |
03-22-2004, 11:25 PM | #2 (permalink) |
Follower of Ner'Zhul
Location: Netherlands
|
This sounds like a "do my homework for me please" post...
It isn't very difficult if you imlement the factorial computational recursively, but why would any sane man/woman need a factorial calculator in Java? EDIT: actually... thinking about it... I was given an assignment exactly like this in my first year... or was it second?
__________________
The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents. - Nathaniel Borenstein |
03-23-2004, 02:31 PM | #5 (permalink) |
Crazy
Location: Long Island, NY
|
Ok so here is what I have so far... It's not giving me the correct answer and I can't get it to loop... I also have to try to make it so when a user inputs -999 the program will stop...
package proj3laa; import javax.swing.*; import java.text.*; public class Factorial { public static void main(String[] args) { int num; int factorial; factorial=1; num=1; String s1; s1=JOptionPane.showInputDialog("Input a number"); num=Integer.parseInt(s1); while (num>=0 && num!=-999) {//start while factorial= num*(num-1); System.out.print(factorial); num--; }//end while } } *edit- well I figured out how to allow a user to input a number and then the prog outputs all of the numbers below it. (ex user inputs 5 prog outputs: 543231) But i dont know how to get it to multiply those numbers. or anywhere close to what I need it to do besides that.... int factorial; String s1; factorial=1; s1=JOptionPane.showInputDialog("Input a number"); factorial=Integer.parseInt(s1); while (factorial >0) {//start while System.out.print (factorial); factorial--; }//end while
__________________
"Can't help it if I space in a daze, my eyes tune out the other way... I may switch off and go in a daydream... in this head my thoughts are deep, Sometimes I can't even speak, would someone be and not pretend, I'm off again in my world" Last edited by daydream; 03-23-2004 at 03:43 PM.. |
03-23-2004, 04:25 PM | #6 (permalink) |
Crazy
Location: Long Island, NY
|
oh wait... ok so I think that I need to use a for loop for this instead... I tried something like what cubal said...
int n; i=n; int total = 1; for (i>n;i>0;i--) total = total*i; but just writing that isn't working... I'm getting a red line for an error under the n in the for loop. is this right? am i missing something?
__________________
"Can't help it if I space in a daze, my eyes tune out the other way... I may switch off and go in a daydream... in this head my thoughts are deep, Sometimes I can't even speak, would someone be and not pretend, I'm off again in my world" |
03-23-2004, 04:40 PM | #7 (permalink) |
Crazy
|
Are you writing this program for a class? If so, you should speak to whoever is teaching it because you are making basic syntax mistakes.
http://www.eimc.brad.ac.uk/java/tuto...ect/2/loop.htm Go to that link and read about looping. Go from there.
__________________
"Even if you prove me wrong, I'm not going to believe you." - A. McGill |
03-24-2004, 11:00 AM | #8 (permalink) | ||
Free Mars!
Location: I dunno, there's white people around me saying "eh" all the time
|
Here's your anwer:
int i; for(i = n; i > n; i--) i*=i; System.out.println("Factorial Result: " + i); Here's the problem with your program: Quote:
For(set variables; set limits of the variable; iteration) for example for(int i = 0; i < 20; i++) { System.out.println(i); } I don't understand why you need to assign i = n, nor do I see any need to have total = 1. You can still use i to get the result. Quote:
__________________
Looking out the window, that's an act of war. Staring at my shoes, that's an act of war. Committing an act of war? Oh you better believe that's an act of war |
||
03-29-2004, 02:17 PM | #9 (permalink) |
Psycho
Location: London...no longer a student
|
now i'm sure swing was never a first/second term module. i'm pretty sure this is just a command line program that u want, and in that case get rid of your JOptionPane etc.
__________________
"Never underestimate a dumb question"-- Brandon Boyd |
03-29-2004, 02:22 PM | #10 (permalink) |
Psycho
Location: London...no longer a student
|
i think it was something like this, any mistakes sorry, i just tried to knock it up quick for u:
if (n <0) { System.out.println("Error, negative number !"); System.exit(-1); } //check exit condition if (n == 999) { System.out.println("Exiting"); System.exit(1); } // Check for zero if (n == 0) { System.out.println("The factorial of zero is one."); System.exit(-2); } // Compute for ( count = 1; count <= n; count++) { fact*= count; } // print result System.out.println("The factorial of " + n + " is : " + fact + "."); all u need to do is take in the user input from the command line with args[0] and check what the number is
__________________
"Never underestimate a dumb question"-- Brandon Boyd |
04-02-2004, 12:03 PM | #11 (permalink) |
Crazy
Location: Long Island, NY
|
thanks a lot for your help everyone! I finally figured out my program!
package proj3; import javax.swing.*; import java.text.*; public class factorial { public static void main(String[] args) { String s1; int num1,total; num1=0; total=1; s1=JOptionPane.showInputDialog("Enter a number to calculate its factorial.\nEnter -999 to exit the program"); num1=Integer.parseInt(s1); while(num1!=-999) {//start while loop 1 System.out.print(num1 + "!="); while ((num1 > 1) && (num1 != -999)) { //start of while statement 2 System.out.print(num1 + "*"); total = (total * num1); num1--; } //end of while statement 2 System.out.print("1="); System.out.println(total); System.exit(0); }//end while loop 1 } }
__________________
"Can't help it if I space in a daze, my eyes tune out the other way... I may switch off and go in a daydream... in this head my thoughts are deep, Sometimes I can't even speak, would someone be and not pretend, I'm off again in my world" |
Tags |
java, program, w or small |
|
|