Java每n行添加换行符 [英] Java append newline every n lines
本文介绍了Java每n行添加换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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()
次数。我正在做的是在每一行之后添加它,但我只希望它在设定次数后创建它。
I am trying to add a new line to the output, every number.getValue()
amount of times. What I'm doing adds it after every line, but I only want it to create it after a set number of times.
示例:3次
Harry
John
Jake
Amanda
Holly
Sam
推荐答案
在for循环中添加 if
语句,在每个n行输出后打印新行(我相信这是你想要的,而不是在n次迭代之后)。让我们说你想要每3行新行
伪代码:
Just add if
statement in your for loop that prints new line after every n-lines of outputs (I believe that is what you wanted, not after n-iterations). Lets say you want new line every 3 lines
Pseudo code:
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
}
这篇关于Java每n行添加换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文