# Thread: I need help with controlling loops and cutting them off

1. Member
Join Date
Sep 2012
Posts
68
Rep Power
0

## 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. Member
Join Date
Oct 2012
Location
Tempe, Arizona
Posts
77
Blog Entries
12
Rep Power
0

## 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. Member
Join Date
Oct 2012
Location
Tempe, Arizona
Posts
77
Blog Entries
12
Rep Power
0

## 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. ## Re: I need help with controlling loops and cutting them off

Originally Posted by penguinCoder
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

5. Member
Join Date
Sep 2012
Posts
68
Rep Power
0

## 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 04:11 AM.

6. ## 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. Member
Join Date
Sep 2012
Posts
68
Rep Power
0

## 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. ## 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.

#### Posting Permissions

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