Results 1 to 3 of 3
  1. #1
    gallimaufry is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Exclamation "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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Here is a very bad way. Just think why is that.

    Java 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);
            }
        }

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 09:26 AM
  2. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  3. <core:forEach var="" begin="+<%=j%>+">???
    By freddieMaize in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2008, 01:20 AM
  4. Replies: 5
    Last Post: 05-14-2008, 01:43 PM
  5. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 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
  •