Hi, I am working in this program that is suppoused to print the firsts numbers from a series of numbers: 1,5,9,13,17...etc.. So, the numbers are jumping from 4 to 4. for example: if the user enters "1",the program prints 1. if the user enters "2", the program prints 1 and 5. and so on.
Jack, What do you have so far? A good approach to starting is to try solving a similar, but simpler problem. For example, can you have the user enter a number and print it out? Can you print out the numbers from 1 to "n" without skipping?
The hint for the "for loop" is probably just what the original poster needed. I did a quick Google for "java for loop" and hit THIS SITE. Try the example on this page and make sure you know how it works. Follow the "next" link to the "while loop" tip.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: May 08, 2006
This is what I've done so far, but, something is missing: this program is suppoused to print the firsts numbers from a series of numbers: 1,5,9,13,17...etc..(as I said before), so, if the user enters, for example: "3", the program should print the first "3" numbers that are: 1,5,9. but, Right now, if the user enters "3", the program only prints: "1". what can i do to solve this? thanks for any response... the code:
Whoever said the problem is in the "for" loop was correct. Go through your "for" loop with a debugger, or use a pencil and say, "When I start the number i is 1, then i becomes . . . " until you work out exactly what is happening in the "for" loop.
Joined: Apr 11, 2005
Thats right, take a look at your for loop. There are many ways to solve this, seems you are not going with the formula approach I mentioned a few posts ago, but with a continual incrementing approach. But if you use an incrementing approach, then you don't want to be incrementing the loop variable, but you will need some other variable, perhaps initialized to 1 to start with, then you can increment by 4 each time you go through the loop.
Your code seems a little ironic, in that you are using complicated things like BufferedReader, and InputStreamReader, and that you don't quite grasp how to solve this loop, or perhaps thats just how you were told to grab data.
Check your for loop. Trace through each loop, paying careful attention to the value of your i variable as you go along, and take a look at your loop condition as well. You'll see why it only prints once when you enter 3.