Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Loops and conditionals

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loops and conditionals

    Hi everybody. First time poster here. I am having trouble with the if/else if statements for my loop. Any feedback is greatly appreciated.

    package 
    //Import scanner library
    import java.util.Scanner;
     
    //Begin Class Main
    public class Main {
     
        //Begin Main Method
        public static void main(String[] args) {
     
            //Declarations
            //Setting loop count to zero
            int loopresponse = 0;
            int loops = 0;
            //declare a variable of type integer for the switch block. This is the number the factorial will work with.
            int factorial;
            //declare a variable of type integer for the menu
            int answer;
            //declare variable for do loop of type string
            int numberinput;
     
     
            //New Scanner Object sc
            Scanner sc = new Scanner(System.in);
     
            //Welcome message
            System.out.print("Welcome to my factorial program!\nPlease choose from the following:\n"
                    + "1. Run program\n2. Exit program\n");
            answer = sc.nextInt();
            System.out.print("This program will determine the factorial value of positive integers. \n"
                    + "The starting number is 1. \nPlease enter an ending integer value: \n");
     
            //Output a menu. 1 to Run program, 2 to exit the program")
            //Assign input to the users choice (receive input)
     
            //Begin do loop
            do {
                //Begin switch/case statement blocks
                switch (answer){
                    case 1: // declare case 1
                        System.out.println("This program will determine the factorial value of positive integers. \n"
                                + "The starting number is 1. \nPlease enter an ending integer value: \n");
                        numberinput = sc.nextInt(); //scan in number
     
                        for (int n=1; n<= numberinput; n++)
                        {
                            factorial = (n*factorial);
                        }
                        break; //end of case 1
     
                    case 2: // declare case 2
                        //Output a statement thanking the user for using program
                        System.out.print("Thank you for using the Utility Calculator!\nGoodbye!\n");
                        //Use the following code to exit program
    		    System.exit(0);
                        break;  //End of case 2
     
                    default: //Declare default case to catch mistakes
                        //Use the following code to exit program
    		    System.exit(0);
                        break;
                } //end switch/case block
     
                System.out.print("Run factorial program again? (Y for Yes, N for No)\n");
                loopresponse = sc.nextInt(); //Receive output as string declared above used in do loop
     
            //End do loop with question. Use code below
            if (loopresponse.equalsIgnoreCase("y")) { //yes
     
            } else if (loopresponse.equalsIgnoreCase "n") { //no
            int loopresponse = 2;
            }
    }//End do loop
     
     
     
        } //End Main Method
     
    }//End Class


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Loops and conditionals

    What is the problem?

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loops and conditionals

    In your codes, your 'while' statement is missing.. that makes an error because a do loop must be paired with a while statement at the bottom... supplying the while statement you can control the loop using your if/else.

Similar Threads

  1. Need Help! For loops and if/else
    By kram in forum Loops & Control Statements
    Replies: 2
    Last Post: February 11th, 2012, 06:22 PM
  2. For loops
    By JCTexas in forum Loops & Control Statements
    Replies: 4
    Last Post: September 21st, 2011, 05:43 PM
  3. help with loops
    By kidza12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2011, 11:42 PM
  4. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 12:38 PM
  5. need help with loops plz
    By Kilowog in forum Loops & Control Statements
    Replies: 4
    Last Post: September 28th, 2009, 08:11 AM