Results 1 to 8 of 8
  1. #1
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default I need help with controlling loops and cutting them off

    Heres the program description the instructor gave us for class...

    Program Description: Write a program that finds the factorial of a value entered by the user. To calculate 12! (12 factorial) for example the program should calculate the value of

    1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x 11 x 12.

    The program should continue processing numbers until the user inputs a zero. Be sure not to process the terminal value (zero).

    When you are finished submit the completed program to your instructor.

    and here is the code ive got so far
    Java Code:
    import java.io.*;
    import java.util.Scanner;
     
    public class Prog162a
    {
    	public static void main(String args[])
    	{
    		System.out.println("Enter a Number [Zero to quit]");
    		Scanner a = new Scanner(System.in);
    		int z = 0;
    		int b = 0;
    		int aa = a.nextInt();
    		while (aa<=aa * 12 +1)
    		{
    			b = aa*1;
    			continue;
    		}
    			b = aa*2;
    			continue;
     
     
     
     
    		System.out.println("The value of " + aa +"! is" + b);	
    	}
    }
    the think im having trouble with is getting the numbers to multiply differently each time and having the user cut it off in time. Could you help me out please

  2. #2
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: I need help with controlling loops and cutting them off

    First off you should always use descriptive variable names; but that is just a personal pet peeve of mine. Second, you are going to need to wrap up the whole thing, prior to asking the user to enter a number, so that you can repeat it. Make it keep looping as long as the user does not input a 0.

    Next, for computing the factorial. Since you know, or can compute, the number of times that it will have to loop, it is best to use a 'for' loop. Also, remember when you are computing a sum that keeps adding up, you can't just have:
    Java Code:
    sum = increment;
    You have to set it up like:
    Java Code:
    sum += increment;
    If you don't set it up like this, it will just keep overwriting the previous answer, and you will not get a rolling sum. Also, set it up so that you use the variable that is initialized for your 'for' loop for your equation. Each time this number increases by one, and so do the numbers that you want to multiply together.

  3. #3
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: I need help with controlling loops and cutting them off

    Also, just a FYI thing. I am a firm believer that if you are using 'continue' or 'break', you are usually doing something wrong. You should very rarely, if ever, need to use them; unless you were solving this problem using recursion, or something along the lines of that, you shouldn't need to worry about them.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: I need help with controlling loops and cutting them off

    Quote Originally Posted by penguinCoder View Post
    I am a firm believer that if you are using 'continue' or 'break', you are usually doing something wrong.
    Were you infected by the Niclaus Wirth virus?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: I need help with controlling loops and cutting them off

    Ok well i got the code done now i just need help with the cutoff when the user enters 0, thats what is tripping me up now.
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class Prog162a
    {
    	public static void main(String args[])
    	{
    		System.out.println("Enter a Number [Zero to quit]");
    		Scanner a = new Scanner(System.in);
    		int aa = a.nextInt();
    		int x = 1;
    		
    				
    			      while( aa > 0  ){
    			         System.out.print("value of aa : " + x );
    			         x++;
    			         System.out.print("\n");
    			      }
    			   
    		Scanner b = new Scanner(System.in);
    		
    		System.out.println("The value of " + aa +"! is" + aa);	
    	}
    }
    I forgot to take the last scanner out it was me trying to figure it out... didnt work
    Last edited by MrPosotive; 10-12-2012 at 05:11 AM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: I need help with controlling loops and cutting them off

    You will need two loops. One nested inside the other. The outer loop keeps prompting user for a value. Inner loop calculates the factorial. Don't forget you will need to ask the user to enter another value inside the outer loop. At the moment you only ask the user once outside the loop.

  7. #7
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: I need help with controlling loops and cutting them off

    Ok well i figured out how to do the input 0 but i have run into another little snag. now the loop dosent want to run with the new scanner in the loop, it will go once but it is almost like it gets stopped at the scanner.
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class Prog162a
    {
    	public static void main(String args[])
    	{
    		System.out.println("Enter a Number [Zero to quit]");
    		Scanner a = new Scanner(System.in);
    		int aa = a.nextInt();
    		int x = 1;  
    		int bb = 1;	
    			      while( bb == 0  ){
    			         System.out.print("value of aa : " + x );
    			         x++;
    			         System.out.print("\n");
    			         Scanner b = new Scanner(System.in);
    			         bb = b.nextInt();
    			      }
    			     
    		
    		System.out.println("The value of " + aa +"! is" + aa);	
    	}
    }

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: I need help with controlling loops and cutting them off

    You should not need another Scanner object. You can just reuse the one you created outside the loop. To track down your problem use the poor mans debugger. That means put print statements that output different words throughout your code. That way you can see exactly where your code messes up.

Similar Threads

  1. Replies: 4
    Last Post: 06-13-2012, 06:21 AM
  2. JScrollPane is Cutting off my Image upon Scrolling
    By Ryan.Vincent in forum AWT / Swing
    Replies: 6
    Last Post: 04-19-2012, 06:16 AM
  3. Cutting .wav file
    By qxc in forum Advanced Java
    Replies: 0
    Last Post: 06-27-2010, 10:50 AM
  4. Cutting parts of an image and reordering them
    By pedjacar in forum Java 2D
    Replies: 2
    Last Post: 10-16-2008, 10:46 AM
  5. controlling GC
    By ravian in forum Eclipse
    Replies: 2
    Last Post: 01-03-2008, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •