Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with getting user input for my factorial method

  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default Help with getting user input for my factorial method

    Hello everyone! So here is my issue, I am using a while loop for a method to calculate the factorial of a number entered by the user. The while loop works correctly if have it inside my main (and class) but when I try to make a static method, I cant get user input. The reason why I am moving under my main is because i will eventually use a for loop to sum each iteration and i cant do that when it is together (at least i cant find a way). Ok here is my code...

     import java.util.Scanner;
     
    public class whileLoopFac
    {
    public static void main(String args[])
    {
    Scanner input = new Scanner (System.in);
    System.out.println("Please enter a number the number of iterations: ");
    int n= input.nextInt(); //store the number that user inputs
    int callingMethod = Factorials(n);//calling Factorials method
     
     /*
    for(int i=0; i<value; i++)//go through while loop as many times as iterated
    	{
     
     
     
    	}//end loop
     
    	 */
     System.out.println("The factorial is: " + callingMethod );
     
     
    	}//end main
     
    public static int Factorials(int k) 
    {
     
    	int total = 1; 
    	//int input = n;//i want this to be user input HERE IS THE PROBLEM!!
    	while(n > 0)
    	{
    	total *= n; 
    	n--; //decrement number entered by user
     
     
    	}
    	return total;
    } //end SumFactorials
     
    }//ends class
    When I compile, it is unable to know what n is (which has been defined in the main method) I have tried to move that into my static method but had no luck... Thankyou!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with getting user input for my factorial method

    See Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)
    The variable n is out of scope outside the main method, so you cannot reference it from another method. That being said it's value is passed to the factorial method as the parameter name k, so a) use k or b) change the parameter name to n

  3. The Following User Says Thank You to copeg For This Useful Post:

    u-will-neva-no (April 4th, 2011)

  4. #3
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default Re: Help with getting user input for my factorial method

    That worked perfectly lol. Thankyou!

Similar Threads

  1. user input strings to vectors
    By vudoo in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 7th, 2011, 10:30 PM
  2. Game requesting user input
    By Neo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 10:00 PM
  3. Allowing user to input variable.
    By That1guy in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 11:30 PM
  4. Help with toString and accesing user input
    By waltersk20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 07:58 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM