Results 1 to 6 of 6
  1. #1
    MUN39 is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default MultiTable with a loop!! help please

    can some one help me i have worked out the code i need using the if and else statements and a loop but im asking the user to enter a number between 1 and 100 and if they enter a number in hat range then it prints out a multi table for that number but if they enter a number out of range (say 101) i want the program to display a message just once but its not its repeating it 12 times !!

    can some one help

    this is my code

    import java.util.Scanner;

    /*
    */

    class MultiTable
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner (System.in);

    int Count, chooseNumber; // variables things that change

    System.out.println("Please enter a number between 1 and 100 ");
    chooseNumber = input.nextInt();

    for (Count = 1; Count <= 12; Count++)

    {
    if ( chooseNumber >= 1 && chooseNumber <= 100 )
    System.out.println( Count + " * " + chooseNumber + " = " + (Count * chooseNumber));

    else
    System.out.println("The number you have entered is out of range" );

    }

    }
    }

    thanks

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    It is because you have the if statement inside the for loop so it will execute it every time around the loop. Perhaps you should have it the other way around.

  3. #3
    MUN39 is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    How do I rectify that then?

    Thanks

  4. #4
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Check the condition, before you start the loop. If the condition is satisfied, start the loop otherwise not.
    Swastik

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can also accomplish this with a break; after the error message, or set count so it won't meet the loops condition

  6. #6
    Leonidas300 is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default

    at your else body you can enter the following instruction:
    Count = 13;

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  2. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM
  3. Convert do while loop to for loop
    By sandeeptheviper in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:37 PM
  4. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  5. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •