Friday, 16 September 2011

Java program help i always choose best answer?

The factorial of n (written n!) is the product of the integers between 1 and n. Thus

4! = 1*2*3*4 = 24. By definition, 0! = 1. Factorial is not defined for negative numbers.



1. Write a program that asks the user for a non-negative integer and computes and prints

the factorial of that integer. You'll need a while loop to do most of the work隆陋this is

a lot like computing a sum, but it's a product instead. And you'll need to think about

what should happen if the user enters 0.



2. Now modify your program so that it checks to see if the user entered a negative number.

If so, the program should print a message saying that a nonnegative number is required

and ask the user the enter another number. The program should keep doing this until the

user enters a nonnegative number, after which it should compute the factorial of that

number.

Hint: you will need another while loop before the loop that computes the

factorial. You should not need to change any of the code that computes the factorial!



this is what I got so far please help I've been working on this all night //





//************************************鈥?

// Factorials.java

// This application will print out a factorial in response to negative

// and nonnegative numbers.

//************************************鈥?

import java.text.*;

import javax.swing.*;

public class Factorials

{

//------------------------------------鈥?

// Prints the factorial of an integer when nonnegative and negative

// numbers are added.

//------------------------------------鈥?

public static void main (String[] args)

{

int sum=1, integer, Fact;

String x= JOptionPane.showInputDialog (%26quot;Enter an nonnegative number (0 to quit): %26quot;);

Fact = Integer.parseInt(x);

while(Fact %26lt; 0.0)

{

x= JOptionPane.showInputDialog (%26quot;Enter an nonnegative number (0 to quit):%26quot;);

Fact = Integer.parseInt(x);

}

while (Fact %26gt; 0.0);

{sum *= Fact;

Fact--;

}

if (Fact==0)

System.out.println(%26quot;%26quot;);

if (Fact%26gt;0)

System.out.println(%26quot;The factorial is %26quot;+ sum);

}

}

3 hours ago - 4 days left to answer.

Additional Details

this print :



enter a negative number, 0 to quit





but when i put a number ex 7 it prints nothing else
Java program help i always choose best answer?
In the first condition you compare an integer to a double. It should be an integer-to-integer comparison. Immediately after receiving the user's input, confirm that the integer is positive. If yes, calculate and display the factorial, otherwise display an error message. Immediately after displaying the factorial you can end the program. I'd give you an example but your 99% done.