简体   繁体   English

重复数字序列

[英]Repeating Sequence of numbers

public static void main(String[] args){

int a;
for(a = 1; a < 100; a++){

    int r = a%7;

    System.out.println(r);

}

I wrote this down in Java to get a repeating sequence 1 2 3 4 5 6 0 1 2 3 4 5 6 .我用 Java 写下来得到一个重复序列 1 2 3 4 5 6 0 1 2 3 4 5 6 。 . . . . . .

And what I am wondering is how to pick up and print out one of repeating nubmers in the sequence?我想知道的是如何提取并打印出序列中重复的数字之一?

For example, how do I print out 3rd number in the repeating sequence?例如,如何打印出重复序列中的第三个数字?

Save the numbers in an array and then pick the desired one:将数字保存在数组中,然后选择所需的数字:

int[] arr = new int[100];
for (a = 1; a < 100; a++) {
    int r = a % 7;
    arr[a] = r;
}
int k = 3;
System.out.println(arr[k]); //print the number at index 3;

Simply use mod 7 of the desired index只需使用所需索引的 mod 7

 int yourIndex = 4;

 int yourResult = yourIndex % 7;

Also, always put spaces between operators!此外,始终在运算符之间放置空格! (eg. r = a % 7;) (例如。r = a % 7;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM