Results 1 to 6 of 6
Thread: Multiplication loop
 09202012, 10:53 PM #1Senior Member
 Join Date
 Sep 2012
 Posts
 106
 Rep Power
 0
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 110
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!"); } }
 09202012, 11:10 PM #2
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.
 09212012, 12:09 AM #3Senior Member
 Join Date
 Sep 2012
 Posts
 106
 Rep Power
 0
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 relooping 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 110? 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.
 09212012, 01:08 AM #4
Re: Multiplication loop
"get a new one if needed"
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 110
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 declaredLast edited by Norm; 09212012 at 01:12 AM.
If you don't understand my response, don't ignore it, ask a question.
 09212012, 01:12 AM #5Senior Member
 Join Date
 Sep 2012
 Posts
 106
 Rep Power
 0
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("110 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; 09212012 at 01:17 AM.
 09212012, 01:19 AM #6Senior Member
 Join Date
 Sep 2012
 Posts
 106
 Rep Power
 0
Similar Threads

need help with multiplication
By dakid2 in forum New To JavaReplies: 10Last Post: 03082011, 03:41 AM 
need help with this: multiplication table
By MsIceCold in forum New To JavaReplies: 7Last Post: 07132010, 01:31 PM 
Multiplication table
By rjones215 in forum New To JavaReplies: 3Last Post: 10192009, 04:34 PM 
Help with Multiplication
By phil028 in forum New To JavaReplies: 1Last Post: 12062007, 07:39 PM 
Help with multiplication table
By Albert in forum New To JavaReplies: 1Last Post: 07102007, 04:44 PM
Bookmarks