根据传入的值获取连续的数值

huangapple go评论82阅读模式
英文:

Get consecutive values based on value being passed in

问题

以下是翻译好的内容:

我正在寻找一种方法,可以传入一个值,并从中获取3个值。也就是说,如果我传入值0,打印输出将是0 1 2。因此,实际传入的值加上接下来的2个连续数字。

但问题不仅仅是连续的数字。例如,如果我传入1,我不希望得到1 2 3。而应该如下:

  • 0 被传入,所以打印 0 1 2

  • 现在传入 10 1 2 在上面的调用中已经完成。
    因此,传入 1 将打印 3 4 5

  • 现在传入 20 1 2 3 4 5 在上述调用中已经完成。
    因此,传入 2 将输出 6 7 8,依此类推。

以下代码按预期工作,但以这种方式写出来有点愚蠢,而且传入的值是由外部因素确定的,我事先不会知道。

但是,如果传入的值是例如3,则算法应该计算出:

3 个 3 的倍数已经过去了(索引 0 - 8)。

因此,传入 3 将意味着打印出 9、10、11。

以下代码实现了上述所述的内容。但是不可扩展,因此正在寻找某种循环算法。

class App {

    public static void main(String[] args) {

        App app = new App();
        app.printIndex(0); // 目标是打印传入的索引及其后续的2个数字,因此输出 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);
    }
}
英文:

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);
    }
}

答案1

得分: 1

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);
}
英文:
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);
}

huangapple
  • 本文由 发表于 2020年10月26日 04:41:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64528568.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定