# Looping Question

• June 29th, 2010, 11:41 PM
miss confused
Looping Question
Consider the sequence 1, 4, 7, 10, 13. The sequence starts at 1, and adds 3 to get the next value. Write a method called upByThrees, which prints the first n numbers in this sequence in a column.

For example, upByThrees(4) should print:
1
4
7
10

My code is:
int N;
N = 1;
while (N < n)
{
System.out.println(N);
N = N+3;
}

How do you get it to print out x amount of numbers?
• June 30th, 2010, 02:32 AM
Charlie
Re: Looping Question
Writing x ammount of numbers is easy, just line up commands. What you want to do it write 'n' x ammount of times. Its a good idea to always think the way you want your program to run, helps a lot.

A good loop to use for these kinds of situations is the for-loop. The for loop can be used to do something "while this variable passes this check, else do this". For example.

Code :

```int x = 5; for(int i = 0; i < x; i++){ System.out.println(i); }```

every time this is run, x will be printed, then i will become i = i + 1 (i++ does that) and then if i < x, it will run again.

Basically, the print for this should be:

0
1
2
3
4
• June 30th, 2010, 03:30 AM
Faz
Re: Looping Question
Quote:

Originally Posted by miss confused
Consider the sequence 1, 4, 7, 10, 13. The sequence starts at 1, and adds 3 to get the next value. Write a method called upByThrees, which prints the first n numbers in this sequence in a column.

For example, upByThrees(4) should print:
1
4
7
10

My code is:
int N;
N = 1;
while (N < n)
{
System.out.println(N);
N = N+3;
}

How do you get it to print out x amount of numbers?

OK what are you being asked to do? Where are you setting n and what determines it? upByThrees(4) What does this tell you? Is that your whole code? Why would you want to get it to print out x number of times where is there any mention of x?

A few quick tips though don't call your only 2 variables n and N it looks bad and you could end up confusing yourself. Also N += 3 will do the same as N = N + 3
• June 30th, 2010, 06:56 AM
ArunUOM
Re: Looping Question
Code :

```import java.io.*; public class Test { public static void main(String[] args) { BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); PrintWriter stdOut = new PrintWriter(System.out); stdOut.write("Enter n : "); stdOut.flush();   try { int n = Integer.parseInt(stdIn.readLine());   //This part (the for loop) is what should be most interesting to you for(int i=1;i<n*3;i+=3) System.out.println(i); } catch(IOException e) { System.out.println("Invalid"); } finally { try { stdIn.close(); stdOut.close(); } catch(IOException e) { System.out.println("Cannot close open streams !"); } } } }```
• June 30th, 2010, 07:37 AM
miss confused
Re: Looping Question
Quote:

Originally Posted by Faz
OK what are you being asked to do? Where are you setting n and what determines it? upByThrees(4) What does this tell you? Is that your whole code? Why would you want to get it to print out x number of times where is there any mention of x?

A few quick tips though don't call your only 2 variables n and N it looks bad and you could end up confusing yourself. Also N += 3 will do the same as N = N + 3

I'm being asked to print out in lets just say upByThree(4) four of the numbers in the following sequence of my code. With my code, I can either write that code up to a certain number or have it less than n (which I understand is more confusing, but I can change that later).

The code:
Code :

`public void upByThrees(int n) {`

is already given by the problem. The solution is proven right or wrong by inserting a number to variable n.

So with my equation:
Code :

```int N; N = 1; while (N < n) { System.out.println(N); N += 3; }```

Feedback:

The input value, n:
6

1
4

The correct solution produced:
1
4
7
10
13
16
• June 30th, 2010, 08:05 AM
Faz
Re: Looping Question
The reason you only got 1 4 is because you are setting the loop to run until N > n while what you want to to is run it n times I would recommend you use a for loop.

Code :

`for(int i = 1; i <= n; i++)`
That will run n times and you just need to put the code in there to do what needs to be done(increment by 3 and print it out)

For loops are the way to go when you know how many times you want it to run(while you don't exactly know here you the programme will once n is set so it will work).
• June 30th, 2010, 08:30 AM
miss confused
Re: Looping Question
Code :

`for(int i=1; i<=n; i++){`
prints out n amount of

Code :

`for(int i=1; i<=n; i+=3){`
prints out until n

Code :

```for(int i=1; i<=n; i++){ System.out.println(i); i+=3; System.out.println(i); }```
prints out something skewed

i'm just finding everything but the right solution.
• June 30th, 2010, 08:41 AM
Faz
Re: Looping Question
Did I say print out i?
• June 30th, 2010, 08:50 AM
Faz
Re: Looping Question
Quote:

Originally Posted by miss confused
Code :

`for(int i=1; i<=n; i++){`
prints out n amount of

Code :

`for(int i=1; i<=n; i+=3){`
prints out until n

Code :

```for(int i=1; i<=n; i++){ System.out.println(i); i+=3; System.out.println(i); }```
prints out something skewed

i'm just finding everything but the right solution.

Let me hazard a guess as to what they do when n = 6 the first prints 1,2,3,4,5,6 the second prints 1,4 the third prints1,4,5,8 or something like that i is just an integer to keep track not the actuall number. I repeat what are you trying to do? Print out the first n numbers of a sequence not print out the numbers up to n as you keep trying to do.
• June 30th, 2010, 12:46 PM
javapenguin
Re: Looping Question
Quote:

Originally Posted by miss confused
Consider the sequence 1, 4, 7, 10, 13. The sequence starts at 1, and adds 3 to get the next value. Write a method called upByThrees, which prints the first n numbers in this sequence in a column.

For example, upByThrees(4) should print:
1
4
7
10

My code is:
int N;
N = 1;
while (N < n)
{
System.out.println(N);
N = N+3;
}

How do you get it to print out x amount of numbers?

is upByThree in the same class or in a separate class?

If same class, put it after main method like this:

put int x = 0 in main method.
public static int upByThree( int x)
{
x = console.nextInt();
return x;
}

For x times, you, in main method:

x = upByThree(x);

int N = 0;
int otherVariable = 1;

while (N > x)
{
System.out.println(otherVariable);
N = N +1;
otherVariable = otherVariable + 3;
}

If in a separate class have upByThree but have it take no parameters as you can have it defined in the method. I think you need to have a this if you the same name as a private variable, or something along those lines.

Then in the main method in your other class:

separateClass referenceVariable = new seperateClass();

int x = referenceVariable.upByThreei();

then do as shown before with while loop.