# Multiplication loop

• 09-20-2012, 11:53 PM
rru96
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

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!");     }   }```
• 09-21-2012, 12:10 AM
Norm
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.
• 09-21-2012, 01:09 AM
rru96
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.
• 09-21-2012, 02:08 AM
Norm
Re: Multiplication loop
Quote:

"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.

Quote:

``` 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!");   }  }```