Ok this is a simple program and should work..... I cannot figure out why it doesn't.
The user inputs a number, a for loop is initiated to calculate the factorial of the number, and prints the value to the screen.
IF the number is negative a message is displayed that the user must enter a positive number (MY MESSAGE DISPLAY DOESN'T WORK IF A NEGATIVE NUMBER IS ENTERED.... it just sits there and nothing happens).
Any help is appreciated.
import java.util.*;
import javax.swing.*;
public class Factorial {
public static void main(String[] args) {
int n; // variable to store number for which the factorial is to be calculated
int counter; // loop counter
int factorial; // factorial value
Scanner scan = new Scanner(System.in);
System.out.println(
"This program asks the user for a positive integer number,"
+ "\ncomputes the factorial value, and displays the answer."
+ "\nPlease enter a positive integer: ");
n = scan.nextInt();
if (n <= 0)
{
JOptionPane.showMessageDialog(null, "You must enter a positive number.");
}
factorial = 1;
for (counter = 1; counter <= n; counter++)
{
factorial *=counter;
}
System.out.println("The factorial of " + n +" is " +factorial);
}// end method main
}// end class Factorial
The user inputs a number, a for loop is initiated to calculate the factorial of the number, and prints the value to the screen.
IF the number is negative a message is displayed that the user must enter a positive number (MY MESSAGE DISPLAY DOESN'T WORK IF A NEGATIVE NUMBER IS ENTERED.... it just sits there and nothing happens).
Any help is appreciated.
import java.util.*;
import javax.swing.*;
public class Factorial {
public static void main(String[] args) {
int n; // variable to store number for which the factorial is to be calculated
int counter; // loop counter
int factorial; // factorial value
Scanner scan = new Scanner(System.in);
System.out.println(
"This program asks the user for a positive integer number,"
+ "\ncomputes the factorial value, and displays the answer."
+ "\nPlease enter a positive integer: ");
n = scan.nextInt();
if (n <= 0)
{
JOptionPane.showMessageDialog(null, "You must enter a positive number.");
}
factorial = 1;
for (counter = 1; counter <= n; counter++)
{
factorial *=counter;
}
System.out.println("The factorial of " + n +" is " +factorial);
}// end method main
}// end class Factorial