I'm unsure how to use the static method in this program. I am coding a program that will calculate a factorial of a number between 0 and 10. The user can enter the number and the program should calculate the factorial. I had originally written a functional program with all of the code coming from the main and then when I double checked the assignment rubric I noticed I was supposed to place the calculation for getting the factorial in a static method. I believe my issue is towards the bottom where I'm asking the user to enter the number I don't send it to the calculator. I guess I'm unclear on how that is done. I'm new so I apologize for my poor coding and I appreciate any help.
Here is my code:
import java.util.Scanner; //import scanner class
public class FactorialCalculator {
public static long calculator(long fact, int num) {
for(int i = 1; i<=num; i++) {
fact *= i;
}
return fact;
}
public static void main(String[] args) {
Scanner calc = new Scanner(System.in); //create new scanner calc
int num = 0;
long fact = 1;
//welcome user to the Factorial Calculator
System.out.println("Welcome to the Factorial Calculator.");
System.out.println();
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
System.out.println("Please enter an integer that's greater than 0 and less than 10: ");
num = calc.nextInt();
System.out.println("The Factorial for " + num + " is " + fact +".");
System.out.println();
System.out.println("Would you like to continue? y/n");
choice = calc.next();
System.out.println();
}
}
}
6 Answers 6
Place this just before you print the result:
fact = FactorialCalculator.calculator(fact, num)
Comments
You're never calling calculator(long fact, int num);
just call it with
fact = calculator(fact,num);
right after:
num = calc.nextInt();
EDIT:
I would recommend you also remove the long fact as an argument to calculator() and add it into the method body of calculator()
Ex:
public static long calculator(int num){
long fact=1;
for(int i = 1; i<=num; i++){
fact *= i;
}
return fact;
}
1 Comment
You can call a static method of a class as:
variableType variableName = ClassName.MethodName(parameters);
In your case you can simply call:
fact = FactorialCalculator.calculator(fact, num);
It should be inserted somewhere AFTER when you ASK the user for the input num, and BEFORE you print out the fact number to the user
Comments
import java.util.Scanner; //import scanner class
public class FactorialCalculator
{
public static long calculator(int num)
{
long fact = 1;
for(int i = 1; i<=num; i++)
{
fact *= i;
}
return fact;
}
public static void main(String[] args)
{
Scanner calc = new Scanner(System.in); //create new scanner calc
int num;
//welcome user to the Factorial Calculator
System.out.println("Welcome to the Factorial Calculator.");
System.out.println();
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
System.out.println("Please enter an integer that's greater than 0 and less than 10: ");
num = calc.nextInt();
fact = FactorialCalculator.calculator(num);
System.out.println("The Factorial for " + num + " is " + fact +".");
System.out.println();
System.out.println("Would you like to continue? y/n");
choice = calc.next();
System.out.println();
}
}
}
1 Comment
Start with this:
fact = FactorialCalculator.calculator(fact, num)
And you should notice that the program will calculate factorial right for the first time, then it starts to fail since the second time. Fixing that bug is your job.
Comments
As your calculator method is a static method so you can call it directly by using the class name.
num = calc.nextInt();
long result_factorial = FactorialCalculator.calcuator(fact,num)
Your method should be
public static long (int num)
{
long fact = 1;
for(int i = 1; i<=num; i++)
{
fact *= i;
}
return fact;
}
Remove the fact initialisation from your main method.
9 Comments
static and instance method but than i dropped that idea but forgot to remove my sentence.calculator(); not FactorialCalculator.calcuator();FactorialCalculator.calcuator(fact,num)
factis not reset between loops. Keep in mind that fact has to be declared in the calculator methodlong fact=1;as well as inmain().staticmethod can invoke only another static methods from the same class. Yourmainmethod is static, so if you want to callcalculatorit must be static too. You can refer to the static keyword here or to this post.java static keywordand there will be tons of info for you. Maybe you want to learn more of Java keywords and their use :D.