# "else without if"

Printable View

• 10-28-2008, 11:36 AM
gallimaufry
"else without if"
import java.util.*;
public class Factorials
{
public static void main (Strings[] args)
{//Use if and while statements to calculate factorials
// Declaring of variable
int Factorials;
int nextFactorial=1;
int counter;
counter = 1;
Scanner myScanner = new Scanner(System.in);//Allows user input
//to enter an integer greater than 0
Factorials = myScanner.nextInt();//Reads the input
while (Factorials < 0)
{System.outprintln("The factorial of 0 is 1");//Prints if integer entered is 0
else{
while (counter <= Factorials)
{nextFactorial *=counter;//Computers the factorial starting at 1 and counting
up
counter++;}//increment operator
System.out.println("The factorial of " + Factorials +" is: "+
nextFactorial);
//Prints out the factorial of the entered positive integer
}
}
}

I have no idea how to fix it
• 10-28-2008, 11:51 AM
Eranga
Can you explain what's you question is much clearly, I don't think know once wants to run your code and fix all bugs there. I can see lots of basis misses there in your code.
• 10-28-2008, 12:01 PM
Eranga
Here is a very bad way. Just think why is that.

Code:

```    public static void main (String[] args) {         int number; // Holds the user input         int result = 1; // Result         Scanner input = new Scanner(System.in); // Scanner object to get user input                 System.out.println("Enter the number: ");         number = input.nextInt();                 for(int i = 1; i <= number; i++) {             result *= i;             System.out.println(i + "! = " + result);         }     }```