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 10 of 10

Thread: help with code

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

    Default help with code

    I have this school assignment and I just about got it but for some reason I cannot get the continue to let me continue after I click y.

    import java.util.Scanner;

    public class Project4 {

    /**
    *
    * @param args
    */
    public static void main(String[] args) {

    //Welcome user to the program
    System.out.println ("Welcome to the factorial calculator");

    int z;
    double b= 1;
    Scanner sc=new Scanner(System.in);

    String choice = ("y");

    // Get the integer
    System.out.print("Enter an integer that's greater than 0 but less than 9: ");
    z=sc.nextInt();

    // Calculate the factorial

    System.out.println("The factorial is: " +z+ ":");
    for (int i=1 ; i<=z; i++){
    b=b*i;
    }
    System.out.println(b);

    //See if the user wants to continue
    System.out.print("Continue? (y/n): ");
    choice = sc.next();
    System.out.println();

    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with code

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting. Your unformated code is hard to read and understand.

    Where in your code do you test the value of what is read into the choice variable?
    What does it mean for the code to "continue"?

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

    Default Re: help with code

    I don't know how to wrap my code. and about the continue after I get my factorial for the number I put in, i need it to ask me to continue so I can put a new number in to get the factorial of a new number.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with code

    I don't know how to wrap my code.
    wrap your code with
    [code=java] <<<< Put this before
    <YOUR CODE HERE>
    [/code] <<< put this after

    , i need it to ask me to continue
    What does this code do?
      System.out.print("Continue? (y/n): ");
      choice = sc.next();

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

    Default Re: help with code

    import java.util.Scanner;
     
    public class Project4 {
     
    	/**
    	 * 
    	 * @param args
    	 */
    public static void main(String[] args) {
     
    //Welcome user to the program
    	System.out.println ("Welcome to the factorial calculator");
     
    int z;
    double b= 1;
    Scanner sc=new Scanner(System.in);
     
    String choice = ("y");
     
    // Get the integer
    System.out.print("Enter an integer that's greater than 0 but less than 9: ");
    z=sc.nextInt();
     
    // Calculate the factorial
     
    System.out.println("The factorial is: " +z+ ":");
    for (int i=1 ; i<=z; i++){
    b=b*i;
    }
    System.out.println(b);
     
    //See if the user wants to continue
    System.out.print("Continue? (y/n): ");
    choice = sc.next();
    System.out.println();
     
         }
    }

  6. #6
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: help with code

    This should help: while loops allow you to continually iterate through code when a given condition is true. What this means is that if you wrap the factorial part of your code in a while loop, you can have that section of code repeat while a condition is true (so, while your choice variable equals "y").
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

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

    Default Re: help with code

    Can you please give me a tip on how to add it to the program without changing anything but for it to let me continue. I tried to add a while loop and it just puts all the factorials at once and that wont work for my teacher.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with code

    You need to put the while loop around the code that does the fact computation and where you ask the user if he wants to continue. Put the question and the reading of the response inside the loop.
    Exit the loop if the user does not want to stay in the loop.
    For example:
    while(user wants to continue)
    get number
    do computation
    show result
    ask user if he wants to continue
    get user's response
    end loop

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

    Default Re: help with code

    thank you so very much I was able to do it.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with code

    Glad you got it working.

Similar Threads

  1. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  2. Help merging program code with GUI code
    By Wilha in forum AWT / Java Swing
    Replies: 2
    Last Post: January 25th, 2012, 07:03 PM
  3. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  5. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:03 PM