使用循环生成输出?

时间:2018-02-22 06:47:33

标签: java

预期结果:

MYSQL

我的尝试:

(1 - 250)
START = 870136
END   = 870385

(251 - 500)
START = 870386
END   = 870635

(501 - 750)
START = 870636
END   = 870885

(751 - 1000)
START = 870886
END   = 871135

(1001 - 1250)
START = 871136
END   = 871385

(1251 - 1500)
START = 871386
END   = 871635

(1501 - 1750)
START = 871636
END   = 871885

(1751 - 2000)
START = 871886
END   = 872135

2 个答案:

答案 0 :(得分:2)

将要打印的常用内容提取到单独的方法中。

void print(int index){

     int from = index * 250 + 1;
     int to = (index+1) *250;
     int start = BASE +from;
     int end = BASE + to;
     System.out.printf("(%d - %d) START %d END %d", from, to, start, end);

}

其中BASE是您的静态int值

static int BASE =879_886;

您可以使用以下内容进行循环

for (int i = 0; i<8; i++){
    print(i);
}

答案 1 :(得分:2)

请参阅以下代码段

public class Calculation {
    public static void main(String args[]) {
        int start = 870136 - 250;
        int end = 870385 - 250;

        for(int i=1; i<=2000; i=i+250) {
            start = start + 250;
            end = end + 250;

            System.out.println("(" + i + "-" + (i+250 - 1) + ")");
            System.out.println("START = " + start);
            System.out.println("END = " + end + "\n");
        }
    }
}