print

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.

but,

Right now, if the user enters, for example: "3", the program only prints: "1".

what can i do to solve this?

thanks for any response...

the code:

import java.io.*;

publicclass Tor{

publicstaticvoid main(String[]args)throws IOException{

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

int inp;

System.out.println("Enter the number:");

inp=Integer.parseInt(br.readLine());

int i;

System.out.println("The numbers:");

for ( i=1; i<=inp; i+=4 ){

System.out.println(i);

}

}

}

[1560 byte] By [deroka] at [2007-11-27 4:28:25]
# 1
If the user enters 3, then inp = 3. The loop starts at i at 1, and prints that out. Then it adds 4 to i, making it 5. Then it checks that i <= inp, (5<=3) which it isn't, so it breaks out of the loop without printing anything else.
hunter9000a at 2007-7-12 9:37:10 > top of Java-index,Java Essentials,Java Programming...
# 2

> If the user enters 3, then inp = 3. The loop starts

> at i at 1, and prints that out. Then it adds 4 to i,

> making it 5. Then it checks that i <= inp, (5<=3)

> which it isn't, so it breaks out of the loop without

> printing anything else.

i understand this,

but still can't figure out how to do it.

deroka at 2007-7-12 9:37:10 > top of Java-index,Java Essentials,Java Programming...
# 3

> > If the user enters 3, then inp = 3. The loop

> starts

> > at i at 1, and prints that out. Then it adds 4 to

> i,

> > making it 5. Then it checks that i <= inp, (5<=3)

> > which it isn't, so it breaks out of the loop

> without

> > printing anything else.

>

> i understand this,

> but still can't figure out how to do it.

you could keep a counter that tells you how many numbers you have gone through seperate of the inp.

Aknibbsa at 2007-7-12 9:37:10 > top of Java-index,Java Essentials,Java Programming...
# 4
I would do it this way close to this:First assign a new int the value of inp. Inp should not change.At the start print out of the value of the new int. As you do your for loop, add +4 onto the new int at the very end.Message was edited by: jjhusa01
jjhusa01a at 2007-7-12 9:37:10 > top of Java-index,Java Essentials,Java Programming...