String[] names = new String[18];
names[0] = "James Raider";
names[1] = "Calm Hain";
names[2] = "Ewds Ashby";
names[3] = "Gedge Taylor";
names[4] = "Hay Fin";
names[5] = "Ian Hilton";
names[6] = "John Coke";
names[7] = "Nathan Dryer";
names[8] = "Jess Maguire";
names[9] = "Jamie Loyal";
names[10] = "Luke Shwinger";
names[11] = "Parrot Tom";
names[12] = "John Clarke";
names[13] = "Steven Mason";
names[14] = "Shing Lao";
names[15] = "Tom Brook";
names[16] = "Arthitus Pint";
names[17] = "Yifan Yao";
StringBuilder sb = new StringBuilder();
ArrayList<Integer> list = new ArrayList();
for (int i = 0; i < names.length; i++) {
list.add(i);
}
Collections.shuffle(list);
for (int i = 0; i < names.length; i++) {
field.append(names[list.get(i)] + "\n");
for (int u = 0; u < (Integer) number.getValue(); u++) {
field.append("\n");
}
}
我尝试每次number.getValue()
次每次都在输出中添加一个新行。我正在做的是在每一行之后添加它,但我只希望它在经过一定次数后创建它。
示例:3次
Harry
John
Jake
Amanda
Holly
Sam
答案 0 :(得分:1)
这可以为您提供所需的输出。
for (int i = 0; i < names.length; ++i) {
// add name to field
field.append(names[list.get(i)]);
field.append('\n');
// every number.getValue() names, add a new line
if (i % number.getValue() == (number.getValue() - 1)) {
field.append('\n');
}
}
编辑:在编辑原始帖子后,我看到您的示例中丢失了一些格式。相应地更新了我的答案。
答案 1 :(得分:0)
您可以使用mod
来帮助您解决此问题。像这样:
for (int i=0; i<names.length; i++) {
field.append(names[list.get(i)]+"\n");
if (i%3==0){
//Do Stuff here every 3rd iteration.
}
}
不确定何时要打印,mod
,%
,只需要输入剩余的数字。循环时非常有用。所以在上面的例子中,当if
除以3没有余数时,它只会进入i
。因此对于以下i
:
0 3 6 9 12 15
希望这有帮助!
答案 2 :(得分:0)
只需在for循环中添加if
语句,在每个n行输出后打印新行(我相信这是你想要的,而不是在n次迭代之后)。让我们说你想要每3行新线
伪代码:
int newLine = 3;
int multiplicator = 1;
for(int i = 0; i< names.length; i++){
if( newLine == i){
// print extra line after every 3rd output
field.append("\n");
newLine = 3; //to reset variable to the correct delimiter on each subsequent n-th iteration
multiplicator++;
newLine = newLine * multiplicator;
}
field.append(names[i] + "\n")
// print regular value on normal line during every iteration
}