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!!!

Dani AI

Generated

A short diagnosis and a compact, robust fix.

The main reasons the original program gave wrong output were: the accumulator was initialized to 0 (so every multiplication stayed 0), and the loop used a constant bound and called System.exit inside the loop (so it never completed the intended iterations). demonstrated correct iteration approaches, and correctly noted that exiting inside the loop and using a fixed MAX_VALUE for the loop limit are problems to avoid.

A cleaner approach separates the input value from the running product, initializes the product to 1, and iterates from 2 up to the input. Parsing should be guarded so invalid text is handled gracefully. The loop should not contain any exit call; show the final result after the loop finishes.

Example of just the core computation (keeps names clear and avoids the mistakes above):

int n = Integer.parseInt(response.trim());   // parse with try/catch in real code
int result = 1;
for (int i = 2; i <= n; i++) {
    result *= i;
}
JOptionPane.showMessageDialog(null, "The factorial of " + n + " is: " + result);

Extra notes and cautions: use try { Integer.parseInt(...) } catch (NumberFormatException e) to handle bad input; treat negative inputs as invalid. Be mindful of overflow: int safely holds up to 12! (479,001,600); use long if allowing larger inputs (long holds up to 20!). Avoid calling System.exit(0) in normal flow — returning from main or letting the program end naturally is preferred. This keeps the code readable, testable, and aligned with the learning goals highlighted by while following the working approaches shown by .

Recommended Answers

All 4 Replies

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.

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); 

    }

}

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!)

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.