Note that there are some explanatory texts on larger screens.

plurals
  1. POJava factorial output
    primarykey
    data
    text
    <p>I am supposed to create a program that asks the user for a number and takes the factorial of that number then asks if they want to do another factorial (Y,N).</p> <p>It is supposed to work like this:</p> <ul> <li>Enter a number to take the factorial of: 4</li> <li>4! = 24</li> <li>Do another factorial (Y,N)? Y</li> <li>Repeat until N is entered</li> </ul> <p>My output is like:</p> <ul> <li>Enter a number to take the factorial of: </li> <li>"Do another factorial? (Y,N)?"</li> <li><p>4! = 1 regardless of whether I enter Y or N.</p> <p>Here's my code:</p> <pre><code>import java.util.Scanner; public class factorial { public static void main ( String [] args ) { Scanner input = new Scanner(System.in); System.out.print("Enter a number you want to take the factorial of: "); int num = input.nextInt(); int fact = 1; System.out.printf("%d! = %d\n ", num, fact, Factorial(num, fact)); } public static int Factorial(int num, int fact) { Scanner input = new Scanner(System.in); char foo; System.out.print("Do another factorial (Y,N)?"); foo = input.next().charAt(0); for (int i = 1; i &gt;= num; i++) { fact *= i; if (foo == 'Y') { System.out.print("Do another factorial (Y,N)?"); foo = input.next().charAt(0); continue; } else { break; } } return fact; } } </code></pre></li> </ul> <p>After the change:</p> <pre><code>import java.util.Scanner; public class factorial { public static void main ( String [] args ) { Scanner input = new Scanner(System.in); System.out.print("Enter a number you want to take the factorial of: "); int num = input.nextInt(); int fact = 1; System.out.printf("%d! = %d\n ", num, Factorial(num, fact)); System.out.print("Do another factorial (Y,N)? "); char foo = input.next().charAt(0); while (foo != 'N') { System.out.print("Do another factorial (Y,N)? "); foo = input.next().charAt(0); System.out.print("Enter a number you want to take the factorial of: "); num = input.nextInt(); System.out.printf("%d! = %d\n", num, Factorial(num, fact)); } } public static int Factorial(int num, int fact) { for (int i = 1; i &lt;= num; i++) { fact *= i; } return fact; } </code></pre> <p>}</p> <p>Output still has some problems:</p> <ul> <li>Enter a number to take the factorial of: 4</li> <li>4! = 24</li> <li>Do another factorial (Y,N)? Y</li> <li>Do another factorial (Y,N)? Y</li> <li>Enter a number to take the factorial of: 4</li> <li>4! = 24</li> <li>Do another factorial (Y,N)? N</li> <li>Enter a number to take the factorial of: </li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload