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

Thread: Help with getting a loop to work

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with getting a loop to work

    Hi I am creating a program to give me the muliplication tables between 1 and 12. The assignment as I post below is complete, however I want to be able to prompt the user to enter a correct value without having to re-run the program. Any help is much appreciated...

    import java.util.Scanner;
    public class MultiplicationTables {
    public static void main(String[] args) {
    int userChoice;
    int y;
    int result;

    Scanner input = new Scanner(System.in);
    System.out.println("Please enter an integer between 1 and 12");
    userChoice = input.nextInt();
    {
    if(userChoice== 1 && userChoice <= 12)
    {
    for(y =1; y<= 12; y++)
    {
    result = userChoice * y;
    System.out.println(y +" * " + userChoice +" = " + result);
    }
    }
    else
    {
    System.out.println("Please enter an integer between 1 and 12");
    }
    }
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with getting a loop to work

    Please read the Announcement topic at the top of the sub-Forum to learn how to correctly post code along with other helpful advice.

    To code what you've described, put the desired "repeating code" in a loop of some sort and add a mechanism to the loop that gives the user an exit. A while loop is typically used, but do/while may also be appropriate.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with getting a loop to work

    Make a while loop with an inner if statement and add a sentinel value to exit the loop.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with getting a loop to work

    if(userChoice== 1 && userChoice <= 12)
    If userChoice is 1 then the second condition MUST be true. If the userChoice is false then the entire condition will be false. Therefore the second condition is totally useless.
    Improving the world one idiot at a time!

Similar Threads

  1. Can't get the loop to work properly
    By thegreatzo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 9th, 2012, 02:57 PM
  2. Why won't this for loop work?
    By vwillis in forum Loops & Control Statements
    Replies: 1
    Last Post: October 14th, 2011, 12:49 PM
  3. How to make for loop work?
    By Dragonkndr712 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 8th, 2011, 02:14 PM
  4. I can't get this loop to work correctly
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 27th, 2011, 04:20 PM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM