Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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?  Reply With Quote

3. ## 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.

```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  Reply With Quote

4. ## Re: Looping Question 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  Reply With Quote

5. ## Re: Looping Question

```import java.io.*;
public class Test
{
public static void main(String[] args)
{
PrintWriter stdOut = new PrintWriter(System.out);
stdOut.write("Enter n : ");
stdOut.flush();

try
{

//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 !");
}
}
}
}```  Reply With Quote

6. ## Re: Looping Question 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:
`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:
```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  Reply With Quote

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

`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).  Reply With Quote

8. ## Re: Looping Question

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

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

```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.  Reply With Quote

9. ## Re: Looping Question

Did I say print out i?  Reply With Quote

10. ## Re: Looping Question Originally Posted by miss confused `for(int i=1; i<=n; i++){`
prints out n amount of

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

```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.  Reply With Quote

11. ## Re: Looping Question 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.  Reply With Quote