Assistance with assignment question

Write a program that takes a positive integer from user (use JOptionPane class for both input and output). The program outputs the factorial of that integer. The factorial of the integer n is n * (n-1) * (n-2) * … * 3 * 2 * 1. For instance, the factorial of 4 is 4 * 3 * 2 * 1 = 24.
Your program should allow the user to continue entering different integers for the calculation until -1 is entered. Write the prompt clearly.
Note:
1) You don’t need to handle the wrong input type such as a string. But for any negative integer except -1, you should allow the user to re-enter the input.
2) Must use a ‘for’ loop to calculate the factorial.
3) This program needs nested loops. The outer loop controls the entering input data (positive integer: output the factorial and prompt user for another input; -1: finish the program; other negative integer: prompt user to enter a correct input). The inner ‘for’ loop calculates the factorial of each input data.

Below is furthest i have gotten.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import javax.swing.JOptionPane;

public class Assignment7
{
  public static void main( String [] args )
  {
    String name = JOptionPane.showInputDialog( null,
                  "Please input a positive integer" );

   





  }
}


Any help would be appreciated. Thank You.
I'm still pretty beginner at Java, but this is what I tried:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static void main(String[] args) {
        int stringInput = 0;
        while(true){
            String input = JOptionPane.showInputDialog("Enter a number to find its factorial");
            stringInput = Integer.parseInt(input);
            if(stringInput == -1){
                break;
            }
            while(stringInput < 0){
                input = JOptionPane.showInputDialog("Please enter a positive number");
                stringInput = Integer.parseInt(input);
            }
            int factorial = 1;
            for(int i = 1; i <= stringInput; i++){
                factorial = factorial * i;
            }
            String output = new Integer(factorial).toString();
            JOptionPane.showMessageDialog(null, output);
        }
    }
thank you greatly appreciate it compile and ran perfectly

the first joptionpane while loops works perfectly.
only issue is getting the second joptionpane "please enter a positive number" to come up
and the when i enter a number to be factor i get a 10 number for example 24 comes out to -7759462002

Last edited on
The answer for 24! is too big to fit into an int. A the biggest an int can get to is 2^31 -1. The second JOptionPane only shows up if you enter an integer that is a negative.
Last edited on
Topic archived. No new replies allowed.