I feel like I'm really close... I'm supposed to have some other things configured and use JOptionPane... but I can't get the numbers to output correctly...

import javax.swing.JOptionPane;

public class M3_2E5 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

//declare variables		
final int MIN_VALUE = 0;
final int MAX_VALUE = 10;
int factorial = 0;
String response = null;

//show input box
response = JOptionPane.showInputDialog("Please enter a number between 0 and 10: ");

if (response == null){
	JOptionPane.showMessageDialog(null, "You pressed Cancel.");
	System.exit(0);
}
	
if (response.equals("")){
	JOptionPane.showMessageDialog(null, "You didn't enter anything.");
	System.exit(0);
}

factorial = Integer.parseInt(response);

if ((factorial < MIN_VALUE) || (factorial > MAX_VALUE)){
	JOptionPane.showMessageDialog(null, "You entered an invalid number.");
	System.exit(0);
}
		
if (factorial == 0){
	JOptionPane.showMessageDialog(null, "The factorial of the number you entered is 1.");
	System.exit(0);
}


for (int i = 1; i <= MAX_VALUE; i++)
{	
		factorial *= i;
		JOptionPane.showMessageDialog(null, "The factorial of the number you entered is: " + factorial);
	
		
		
	System.exit(0);
	
}
	

	}

}

Thanks a bunch for anyone that helps!!!

Try this.. It's working now..

import javax.swing.JOptionPane;

public class M3_2E5 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

//declare variables     
final int MIN_VALUE = 0;
final int MAX_VALUE = 10;
int factorial = 0;
String response = null;
int i;

//show input box
response = JOptionPane.showInputDialog("Please enter a number between 0 and 10: ");

if (response == null){
    JOptionPane.showMessageDialog(null, "You pressed Cancel.");
    System.exit(0);
}

if (response.equals("")){
    JOptionPane.showMessageDialog(null, "You didn't enter anything.");
    System.exit(0);
}

factorial = Integer.parseInt(response);

if ((factorial < MIN_VALUE) || (factorial > MAX_VALUE)){
    JOptionPane.showMessageDialog(null, "You entered an invalid number.");
    System.exit(0);
}

if (factorial == 0){
    JOptionPane.showMessageDialog(null, "The factorial of the number you entered is 1.");
    System.exit(0);
}

i=factorial-1;
while(i>0)
{   
        factorial=factorial*i;
        i--;
}
        JOptionPane.showMessageDialog(null, "The factorial of the number you entered is: " + factorial);



    System.exit(0); 

    }

}

Thanks!
Varsha.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

With for loop:

import javax.swing.JOptionPane;

public class M3_2E5 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

//declare variables     
final int MIN_VALUE = 0;
final int MAX_VALUE = 10;
int factorial = 0;
String response = null;
int i,j;

//show input box
response = JOptionPane.showInputDialog("Please enter a number between 0 and 10: ");

if (response == null){
    JOptionPane.showMessageDialog(null, "You pressed Cancel.");
    System.exit(0);
}

if (response.equals("")){
    JOptionPane.showMessageDialog(null, "You didn't enter anything.");
    System.exit(0);
}

factorial = Integer.parseInt(response);

if ((factorial < MIN_VALUE) || (factorial > MAX_VALUE)){
    JOptionPane.showMessageDialog(null, "You entered an invalid number.");
    System.exit(0);
}

if (factorial == 0){
    JOptionPane.showMessageDialog(null, "The factorial of the number you entered is 1.");
    System.exit(0);
}

j=factorial-1;
for(i=j; i>0; i--)
{   
        factorial=factorial*i;
}
        JOptionPane.showMessageDialog(null, "The factorial of the number you entered is: " + factorial);



    System.exit(0); 

    }

}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

varshakite - Doing the guy's homework for him isn't much of a help. Much better to lead him to the answer, so he has to make the fixes himself. That way he learns something from the exercise, and you improve your skills as a writer in the process - you might even learn something from having to explain why a thing works as it does.

So, it's better to point out that the problem is in this code block:

for (int i = 1; i <= MAX_VALUE; i++)
{   
        factorial *= i;
        JOptionPane.showMessageDialog(null, "The factorial of the number you entered is: " + factorial);



    System.exit(0);

}

than to just post a "fixed" version of the program.

If you're really feeling generous, you might maybe point out that a for loop with a constant for its limit is not responsive to the user's input, and in any case a for loop including a command to kill the program will only execute once. If you want to go really deep, you might observe that it's generally not necessary to end a program with an exit(0) - this might even cause a problem if you were to try to do some automated testing later.

Nice to see more people answering questions - please keep it up, just give more suggestions than code.

(also, please put your code in CODE tags for readability!)

Edited 3 Years Ago by pyTony: fixed formating

Thank you all for your help... I was able to figure it out. Sometimes it helps to see an example of something similar that works so that the logic is easier understood...

This article has been dead for over six months. Start a new discussion instead.