Get consecutive values based on value being passed in

Fllappy

I am looking for a way to pass in a value and get 3 values out of it. Meaning if I pass in the value 0, the print out will be 0 1 2. Thus this is actual value passed in plus the next 2 consecutive numbers.

But it's not just the about consecutive numbers. For example, if I pass in 1, I am NOT expecting 1 2 3. Instead it should be as follows.

  • 0 passed in so print 0 1 2.

  • Now passing in 1. 0 1 2 already done with above call. Thus passing in 1 will print 3 4 5.

  • Now passing in 2. 0 1 2 3 4 5 already done with above calls. Thus passing 2 will output 6 7 8 and so on.

The following code works as intended but kinda silly to be writing it out this way plus the value being passed in is determined by external factors and I won't know it beforehand.

But if the value passed in is example 3, the algorithm should calculate that

3 multiples of 3 has passed (index 0 - 8).

Thus passing in 3 would mean print 9, 10, 11.

The following code does whats explained above. But is not scalable thus looking for some sort of looping algorithm.

class App {

    public static void main(String[] args) {

        App app = new App();
        app.printIndex(0); // idea is to print the index being passed in and the next 2 numbers, so 0 1 2
        app.printIndex(1); // 3 4 5
        app.printIndex(2); // 6 7 8
        app.printIndex(3); // 9 10 11
    }

    int getIndex(int i) {
        if(i == 0) return i;

        if(i == 1) return 3;

        if(i == 2) return 6;

        if(i == 3) return 9;

        return 0;
    }

    void printIndex(int i) {

        System.out.println(getIndex(i));
        System.out.println(getIndex(i) + 1);
        System.out.println(getIndex(i) + 2);
    }
}
Luke
int getIndex(int i) {
    return i*3;
}

your getIndex method needs to just return i*3 to get every 3rd number. Your program should work if you just replace this. You could even leave out the getIndex() method entirely if you edit your printIndex() method to

void printIndex(int i) {
    System.out.println(i*3+"");
    System.out.println(i*3 + 1);
    System.out.println(i*3 + 2);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to group consecutive elements values based on the key value in Python 3?

From Dev

Values being passed as strings and not integers

From Dev

Set the value of an unbound DropDownList based on values passed in a querystring

From Dev

Return a Single Value based on 3 values passed into a query

From Dev

Value in Android not being passed to PHP

From Dev

Boolean value not being passed in Angular

From Dev

Value in Android not being passed to PHP

From Dev

GET form not being passed correctly

From Dev

Cumulative sum based on consecutive values

From Dev

Cumulative sum based on consecutive values

From Dev

Indexing based on consecutive field values

From Dev

Find consecutive cells based on a value

From Dev

Companion objects that hold both a default instance AND values that get manipulated before being passed as default arguments -> NPE

From Dev

Get maximum value based on unique values

From Dev

Get count of historical values based on current value

From Dev

xpath expression to get to a value based on other values

From Dev

Hidden type input values not being passed in php

From Dev

values not being passed onto process.php

From Dev

count cells based on value if consecutive values same and more than 6 cells

From Dev

Example of VBA array being passed by value?

From Dev

Return value not being passed to other function

From Dev

pointer changes value without being passed as argument

From Dev

c double argument being passed as wrong value

From Dev

Variable value is not being passed inside the function

From Dev

Are these arguments in Python being passed by value or by reference?

From Dev

Jquery autocomplete value is not being passed to django form

From Dev

Variable value passed to report is not being printed

From Dev

Delete variables based on their duplicate consecutive values in pandas

From Dev

Excel Get minimum value, and If two cells match from a row, return values from the consecutive 3 columns

Related Related

  1. 1

    How to group consecutive elements values based on the key value in Python 3?

  2. 2

    Values being passed as strings and not integers

  3. 3

    Set the value of an unbound DropDownList based on values passed in a querystring

  4. 4

    Return a Single Value based on 3 values passed into a query

  5. 5

    Value in Android not being passed to PHP

  6. 6

    Boolean value not being passed in Angular

  7. 7

    Value in Android not being passed to PHP

  8. 8

    GET form not being passed correctly

  9. 9

    Cumulative sum based on consecutive values

  10. 10

    Cumulative sum based on consecutive values

  11. 11

    Indexing based on consecutive field values

  12. 12

    Find consecutive cells based on a value

  13. 13

    Companion objects that hold both a default instance AND values that get manipulated before being passed as default arguments -> NPE

  14. 14

    Get maximum value based on unique values

  15. 15

    Get count of historical values based on current value

  16. 16

    xpath expression to get to a value based on other values

  17. 17

    Hidden type input values not being passed in php

  18. 18

    values not being passed onto process.php

  19. 19

    count cells based on value if consecutive values same and more than 6 cells

  20. 20

    Example of VBA array being passed by value?

  21. 21

    Return value not being passed to other function

  22. 22

    pointer changes value without being passed as argument

  23. 23

    c double argument being passed as wrong value

  24. 24

    Variable value is not being passed inside the function

  25. 25

    Are these arguments in Python being passed by value or by reference?

  26. 26

    Jquery autocomplete value is not being passed to django form

  27. 27

    Variable value passed to report is not being printed

  28. 28

    Delete variables based on their duplicate consecutive values in pandas

  29. 29

    Excel Get minimum value, and If two cells match from a row, return values from the consecutive 3 columns

HotTag

Archive