我有一个包含数据(A,B,C,D)的“X”变量。并且变量“Y”值10。我想循环“X”一些Y,但是如何对结果进行迭代是
A B C D A B C D A B.
package shiftkerja;
import java.util.Scanner;
/**
*
* @author USER
*/
public class ShiftKerja {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner jml = new Scanner(System.in);
int[] deret = {0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,};
System.out.println("Masukan Jumlah Pekerja: ");
Integer jumlah = jml.nextInt();
System.out.println("Masukan Jumlah Hari: ");
Integer hari = jml.nextInt();
String[] pekerja = new String[jumlah];
for (int i = 0; i < pekerja.length; i++)
{
System.out.println("Pekerja ke "+i+": ");
pekerja[i] = jml.next();
}
System.out.println("-------------------------");
for (int k = 0; k < deret.length; k++)
{
for (String p: pekerja){
System.out.println(p+" akan bekerja selama: ");
}
}
}
您的程序不清楚问题,但您的示例的解决方案可以像下面这样完成:
假设x是数组(X = [A,B,C,D]),y是int。假设xLen持有“X”的长度
int i,j = 0,xLen = x.length;
for(i = 1; i < y; i++){
j = j % xLen;
System.out.println(x[j]);
}