Results 1 to 6 of 6
  1. #1
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    106
    Rep Power
    0

    Default Multiplication loop

    Below is some simple coding I am trying to practice on. I am currently going through Java for Dummies vol. 4 and trying out some simple progs.
    I was expecting this program to:
    1. Ask user for int between 1-10
    2. Take user number and multiply that number times 0, 1, 2, 3, ... each iteration being its own line.
    i.e. 3 times 0 is 0
    3 times 1 is 3
    3 times 2 is 6
    etc.
    3. If the number is NOT between 1 and 10, prompt the user to enter a number between 1 and 10 again.

    ISSUE:
    If the user enters an int between 1 and 10, all works fine.
    If the user enters an int <1 || >10, it prompts for a reenter.
    - The NEXT time, it doesn't matter what the number is. It will multiply the number regardless of range.

    QUESTION:
    Why is the program working the FIRST time through if a user enters an out of range int but not subsequent iterations?
    I feel that I am supposed to be using an 'else if' but still have not fully grasped those. When I try to implement an 'else if' under the current 'if', I get an error. Is the 'else if' supposed to be inside the {} of an if statement. The setup is confusing me somewhat. Thanks

    Java Code:
    import java.lang.System;
    import java.util.Scanner;
    
    public class LoopTest{
    
    
      public static void main(String[] args){
    
        System.out.print("Give me a number ");
        System.out.println("between 1 and 10...");
        Scanner myScanner = new Scanner(System.in);
        
        int inputNumber = myScanner.nextInt();
        
        if((inputNumber >=1) && (inputNumber <=10)){
        for(int count = 0; count <=10; count++){
          System.out.println(inputNumber + " times " + count + " is equal to " + inputNumber*count);
        }
        }
        while((inputNumber < 1) || (inputNumber > 10)){
          System.out.println("Please try again.");
          System.out.println("Please pick a number between 1 and 10");
          int reTry = myScanner.nextInt();      
        
          for(int count = 0; count <= 10; count++){
          System.out.println(reTry + " times " + count + " is equal to " + reTry*count);
          }
        }
      System.out.println("Okay. We're done!");
        }
      }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Multiplication loop

    You have loops that do the computations in two places. Try to change your logic so there is only one loop.
    Get the number, test it and get a new one if needed, before doing the computation.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    106
    Rep Power
    0

    Default Re: Multiplication loop

    I think I am getting lost on the "get a new one if needed" part.

    I understand the System.in to get the initial number. And assigning that to 'inputNumber' in this case. But I guess I am lost on the re-looping if the int is out of bounds. If I ask for another input 'inputNumber', that variable is already declared. If I rename the new variable to something, how do I loop that to go through the math again. It seems like I would have infinite nests. It seems as though I should use. Can you explain a little more on making the program ~reset~ (for lack of better terminology on my part) if the user enters an int that is outside 1-10? Trying to teach myself this and I have read through the Java docs and watched the Java Video Tutorials: Learn Java the easy way!. I just can't get all the wheels turning in the right direction.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Multiplication loop

    "get a new one if needed"
    If less than 1 or greater than 10

    Work on the logic by making a list of the steps (pseudo code) that the program needs to do to solve the problem.
    Start at a high level and then add details.
    Post the list if you have problems getting the logic worked out.

    The original instructions are out of order to me. I would order them this way:
    1. Ask user for int between 1-10
    2.If the number is NOT between 1 and 10, Go back to step 1) & prompt the user to enter a number between 1 and 10 again.
    <When you get here you have a valid number between 1 & 10 inclusive.>
    3. Take user number and multiply that number times 0, 1, 2, 3, ... each iteration being its own line.


    that variable is already declared
    You don't need to define a new variable to read a new value into a variable. You can use an existing variable.
    Last edited by Norm; 09-21-2012 at 01:12 AM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    106
    Rep Power
    0

    Default Re: Multiplication loop

    Still need to look over my code and figure it all out, but thank you for your help. I don't know if this is the way you were talking about it, but it seems to work as far as I can tell.
    Java Code:
     import java.util.Scanner;
    
      
    public class Multiplication{
     
     public static void main(String[] args){
    
          System.out.println("Please give me a number between 1 and 10");
       Scanner myScanner = new Scanner(System.in);
       int userNumber = myScanner.nextInt();
       
    
       while(userNumber<1 || userNumber>10){
         System.out.println("Try again");
         System.out.println("1-10 this time !");
         userNumber = myScanner.nextInt();
    
       }
          if(userNumber>=1 && userNumber<=10){
         for(int i = 1; i <=10 ; i++){
           System.out.println(userNumber + " * " + i + " = " + (userNumber*i)); 
       }
       }
            System.out.println("WooHoo!! You Did It!");
       }
    
     }
    Last edited by rru96; 09-21-2012 at 01:17 AM.

  6. #6
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    106
    Rep Power
    0

    Default Re: Multiplication loop

    That was where I was getting confused. I knew that. but I kept trying to declare a new int variable for the inputNumber instead of asssigning the next input back to that variable. Thank you

Similar Threads

  1. need help with multiplication
    By dakid2 in forum New To Java
    Replies: 10
    Last Post: 03-08-2011, 03:41 AM
  2. need help with this: multiplication table
    By MsIceCold in forum New To Java
    Replies: 7
    Last Post: 07-13-2010, 01:31 PM
  3. Multiplication table
    By rjones215 in forum New To Java
    Replies: 3
    Last Post: 10-19-2009, 04:34 PM
  4. Help with Multiplication
    By phil028 in forum New To Java
    Replies: 1
    Last Post: 12-06-2007, 07:39 PM
  5. Help with multiplication table
    By Albert in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 04:44 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
  •