Java: How can I output the faculty of input number "n"?
Go to solution
Solved by WiiManic,
You'll want to wrap the code in an If/else statement:
import javax.swing.*; public class facultyTask { public static void main(String[] args) { String input; int number; long faculty = 1; input = JOptionPane.showInputDialog(null, "Type in a number n."); number = Long.parseLong(input); if (number > 0) { for(long l = 1; l <= number; l++) { faculty = faculty * l; } JOptionPane.showMessageDialog(null, "faculty of " + input + "! is: " + faculty); } else if(number <= 0) { JOptionPane.showMessageDialog(null, "Input is too small!"); } System.exit(0); } }
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now