In my code I've included an output method with the variables in the parameters, problem is that they're different data types. I know I can convert one to match the other so the code works but I don't see how I can without compromising how the equation works.
I've tried changing factorial from a long variable to an int variable it needs to be a long variable in order for the numbers to reach the max.
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
System.out.println("\tBasic Factorial Calculator");
System.out.println();
int i, number;
long factorial = 1;
number = getNumber();
System.out.println();
output(number, factorial);
}
public static int getNumber()
{
int Num = 0;
boolean done = false;
Scanner input = new Scanner(System.in);
while(!done)
{
try
{
System.out.println("Enter the number you want to find a
factorial of: ");
Num = input.nextInt();
if (Num <= 0) throw new InputMismatchException();
done = true;
}
catch(InputMismatchException e)
{
System.out.println("");
System.out.println("Error, enter a number greater than 0");
input.nextLine();
System.out.println("");
}
}
return Num;
}
public static void output(int i, int Num, long factorial)
{
System.out.println();
for(i = 1; i <= Num; i++)
{
factorial = factorial * i;
}
System.out.println("The factorial of " + Num + " is " +
factorial + ".");
}
}
Just want the user to be able to enter a number then get the factorial of it while including a try catch :/
Your problem was with the output method. Its contructor has three parameters, but you invoke it with two parameters. In fact, you only needed one parameter all along. See my edits.
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
System.out.println("\tBasic Factorial Calculator");
System.out.println();
int number = getNumber();
System.out.println();
output(number);
}
public static int getNumber()
{
int Num = 0;
boolean done = false;
Scanner input = new Scanner(System.in);
while(!done)
{
try
{
System.out.println("Enter the number you want to find a factorial of: ");
Num = input.nextInt();
if (Num <= 0) throw new InputMismatchException();
done = true;
}
catch(InputMismatchException e)
{
System.out.println("");
System.out.println("Error, enter a number greater than 0");
input.nextLine();
System.out.println("");
}
}
return Num;
}
public static void output(int Num)
{
System.out.println();
long factorial = 1;
for(int i = 1; i <= Num; i++)
{
factorial = factorial * i;
}
System.out.println("The factorial of " + Num + " is " +
factorial + ".");
}
}