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.