3 Replies - 560 Views - Last Post: 30 October 2011 - 09:55 AM Rate Topic: -----

#1 iggyman11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-October 11

how to have program continually require input unless negative

Posted 29 October 2011 - 01:28 PM

I know its a simple solution of implementing a while loop I believe, but I cannot figure out where to write it. The goal is to have the program keep asking for user input and then print the factorial until the user inputs a negative in which case the program exits. The code currently calculates a single user input and prints it then terminates.
import java.util.*; 

public class factorial_recursion
{   
    public static int factorial(int user_input)
    {   
        while (user_input >=0)
        {
            if (user_input == 1)
                return 1;
            else
                return user_input*(factorial(user_input-1));
        }
        return factorial(user_input);
    }
    
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a positive integer: ");
        int user_input = input.nextInt();
        int x = factorial(user_input);
        System.out.println(user_input + " factorial is " + x);
        
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: how to have program continually require input unless negative

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: how to have program continually require input unless negative

Posted 29 October 2011 - 01:33 PM

You want to use a while loop in your user input section, not the factorial() method. Either use recursion or a loop to calculate the factorial of the param, but not both.

public static int factorial(int x){
   //calculate x!
}

public static void main(String[] args){

   int input = 0;
   int fact = 0;

   do{
        input = /*get input*/;
        fact = factorial(input);       
   }while(input >= 0);
}


Was This Post Helpful? 1
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: how to have program continually require input unless negative

Posted 29 October 2011 - 02:40 PM

You seem to have a problem understandig the scope of a variable
user_input exists as parameter of the method and then vanishes

The while statement is completly useless, it will be excuted only once
If you have a user_input < 0 you have a infinite loop
    public static int factorial(int user_input)
    {   
        while (user_input >=0)       // if user_input >= 0 enter the while
        {
            if (user_input == 1)     // if user_input == 1
                return 1;            // return and exit the while
            else                     // in all other cases 
                return user_input*(factorial(user_input-1));  // return and exit the while anyhow
        }
        // ok user_input < 0
        return factorial(user_input);   // return factorial(that number < 0
    }


because of the useless while it could be written
    public static int factorial(int user_input)
    {   
        if(user_input >=0)       // if user_input >= 0 enter the while
        {
            if (user_input == 1) 
                return 1; 
            else       
                return user_input*(factorial(user_input-1));
        }
        
        return factorial(user_input);
    }


but it does not solve your infine loop
actually the concept is even easier
    public static int factorial(int user_input)
    {   
        if(user_input <= 1)
           return 1;
        return user_input * factorial(user_input - 1);
    }



Happy coding
Was This Post Helpful? 1
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: how to have program continually require input unless negative

Posted 30 October 2011 - 09:55 AM

You need a loop that's checked at the top:


        int user_input = 0;
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a positive integer (or otherwise to quit): ");

        while (input.hasNextInt() && ((user_input = input.nextInt()) > 0)) {
            int x = factorial(user_input);

            System.out.println(user_input + " factorial is " + x);
        }


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1