mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
Update Factorial.java
The factorial method can be simplified to 2 lines, which I believe is easier to read. Also, in previous version, if user enters two negative numbers back to back, the program crashes. We need a while loop to prompt the user until they actually enter the correct input, instead of only prompting them once. Also, if user enters a character instead of an integer, program crashes with no error message. We should fail more gracefully.
This commit is contained in:
@ -1,10 +1,11 @@
|
|||||||
|
package factorial;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This program will print out the factorial of any non-negative
|
* This program will print out the factorial of any non-negative
|
||||||
* number that you input into it.
|
* number that you input into it.
|
||||||
*
|
*
|
||||||
* @author Unknown
|
* @author Marcus
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Factorial{
|
public class Factorial{
|
||||||
@ -15,23 +16,26 @@ public class Factorial{
|
|||||||
* @param args Command line arguments
|
* @param args Command line arguments
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
Scanner input = new Scanner(System.in);
|
Scanner input = new Scanner(System.in);
|
||||||
//Prompt user to enter integer
|
System.out.print("Enter a non-negative integer: ");
|
||||||
System.out.print("Enter a non-negative integer: ");
|
|
||||||
|
|
||||||
//Proceed with factorial calculation only if inputted number is not negative
|
//If user does not enter an Integer, we want program to fail gracefully, letting the user know why it terminated
|
||||||
if(input.hasNextInt()){
|
try{
|
||||||
int number = input.nextInt();
|
int number = input.nextInt();
|
||||||
if (number < 0){
|
|
||||||
System.out.print("Cannot execute. Please enter a non-negative integer: ");
|
//We keep prompting the user until they enter a positive number
|
||||||
number = input.nextInt();
|
while(number < 0){
|
||||||
} else {
|
System.out.println("Your input must be non-negative. Please enter a positive number: ");
|
||||||
//Output of factorial for any non-negative number
|
number = input.nextInt();
|
||||||
System.out.println("The factorial of "+number+" will yield: "+factorial(number));
|
}
|
||||||
}
|
//Display the result
|
||||||
}
|
System.out.println("The factorial of " + number + " will yield: " + factorial(number));
|
||||||
input.close();
|
|
||||||
}
|
}catch(Exception e){
|
||||||
|
System.out.println("Error: You did not enter an integer. Program has terminated.");
|
||||||
|
}
|
||||||
|
input.close();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recursive Factorial Method
|
* Recursive Factorial Method
|
||||||
@ -40,14 +44,7 @@ public class Factorial{
|
|||||||
* @return The factorial of the number
|
* @return The factorial of the number
|
||||||
*/
|
*/
|
||||||
public static long factorial(int n){
|
public static long factorial(int n){
|
||||||
|
if(n == 0 || n == 1) return 1;
|
||||||
if (n==0){
|
return n * factorial(n - 1);
|
||||||
return 1;
|
|
||||||
} else if (n==1){
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return n * factorial(n-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user