英文:
Java custom separator output for looping
问题
我有这个 for 循环:
for(int i=1; i <= bil; i++){
if (i%2!=0)
System.out.print(i+" ");
}
我想要类似这样的输出(如果 bil = 5):
1, 3 和 5
那么,如何获得这种以逗号为分隔符且最后一个循环使用不同分隔符“和”的输出?
英文:
I've this for loop
for(int i=1; i <= bil; i++){
if (i%2!=0)
System.out.print(i+" ");
}
and i want an output like this (if bil =5)
1, 3 and 5
So, how to get this output that have a comma separator and the last loop have different separator like 'and' ?
答案1
得分: 2
这是一种实现方式:
StringBuilder buf = new StringBuilder();
int lastComma = 0;
for (int i = 1; i <= bil; i++) {
if (i % 2 != 0) {
if (buf.length() != 0) {
lastComma = buf.length();
buf.append(", ");
}
buf.append(i);
}
}
if (lastComma != 0)
buf.replace(lastComma, lastComma + 1, " and");
System.out.print(buf.toString());
不同 bil
值的结果
bil=1 -> 1
bil=2 -> 1
bil=3 -> 1 and 3
bil=4 -> 1 and 3
bil=5 -> 1, 3 and 5
bil=6 -> 1, 3 and 5
bil=7 -> 1, 3, 5 and 7
bil=8 -> 1, 3, 5 and 7
bil=9 -> 1, 3, 5, 7 and 9
bil=10 -> 1, 3, 5, 7 and 9
英文:
Here is one way of doing it:
StringBuilder buf = new StringBuilder();
int lastComma = 0;
for (int i = 1; i <= bil; i++) {
if (i % 2 != 0) {
if (buf.length() != 0) {
lastComma = buf.length();
buf.append(", ");
}
buf.append(i);
}
}
if (lastComma != 0)
buf.replace(lastComma, lastComma + 1, " and");
System.out.print(buf.toString());
Results with various values of bil
bil=1 -> 1
bil=2 -> 1
bil=3 -> 1 and 3
bil=4 -> 1 and 3
bil=5 -> 1, 3 and 5
bil=6 -> 1, 3 and 5
bil=7 -> 1, 3, 5 and 7
bil=8 -> 1, 3, 5 and 7
bil=9 -> 1, 3, 5, 7 and 9
bil=10 -> 1, 3, 5, 7 and 9
答案2
得分: 2
尝试这个。
static void printBil(int bil) {
String separator = "";
for (int i = 1; i <= bil; i++) {
if (i % 2 != 0)
System.out.print(separator + i);
separator = i + 2 >= bil ? " and " : ", ";
}
System.out.println();
}
for (int i = 1; i < 10; ++i) {
System.out.print("bil=" + i + " : ");
printBil(i);
}
输出:
bil=1 : 1
bil=2 : 1
bil=3 : 1 and 3
bil=4 : 1 and 3
bil=5 : 1, 3 and 5
bil=6 : 1, 3 and 5
bil=7 : 1, 3, 5 and 7
bil=8 : 1, 3, 5 and 7
bil=9 : 1, 3, 5, 7 and 9
英文:
Try this.
static void printBil(int bil) {
String separator = "";
for (int i = 1; i <= bil; i++) {
if (i % 2 != 0)
System.out.print(separator + i);
separator = i + 2 >= bil ? " and " : ", ";
}
System.out.println();
}
and
for (int i = 1; i < 10; ++i) {
System.out.print("bil=" + i + " : ");
printBil(i);
}
output:
bil=1 : 1
bil=2 : 1
bil=3 : 1 and 3
bil=4 : 1 and 3
bil=5 : 1, 3 and 5
bil=6 : 1, 3 and 5
bil=7 : 1, 3, 5 and 7
bil=8 : 1, 3, 5 and 7
bil=9 : 1, 3, 5, 7 and 9
答案3
得分: 0
尝试。
var sj = new StringJoiner(", ", "", "");
String tmp = null;
for (int i = 1; i <= bil; i++) {
if (i % 2 != 0) {
if (tmp != null) {
sj.add(tmp);
}
tmp = Integer.toString(i);
}
}
var res = sj.length() > 0
? sj.toString() + " and " + tmp
: sj.add(tmp).toString();
System.out.println(res);
结果:
bil=1 ➡️ 1
bil=2 ➡️ 1
bil=3 ➡️ 1 and 3
bil=4 ➡️ 1 and 3
bil=5 ➡️ 1, 3 and 5
bil=6 ➡️ 1, 3 and 5
bil=7 ➡️ 1, 3, 5 and 7
bil=8 ➡️ 1, 3, 5 and 7
bil=9 ➡️ 1, 3, 5, 7 and 9
bil=10 ➡️ 1, 3, 5, 7 and 9
英文:
Try.
var sj = new StringJoiner(", ", "", "");
String tmp = null;
for (int i = 1; i <= bil; i++) {
if (i % 2 != 0) {
if (tmp != null) {
sj.add(tmp);
}
tmp = Integer.toString(i);
}
}
var res = sj.length() > 0
? sj.toString() + " and " + tmp
: sj.add(tmp).toString();
System.out.println(res);
Result:
bil=1 ➡️ 1
bil=2 ➡️ 1
bil=3 ➡️ 1 and 3
bil=4 ➡️ 1 and 3
bil=5 ➡️ 1, 3 and 5
bil=6 ➡️ 1, 3 and 5
bil=7 ➡️ 1, 3, 5 and 7
bil=8 ➡️ 1, 3, 5 and 7
bil=9 ➡️ 1, 3, 5, 7 and 9
bil=10 ➡️ 1, 3, 5, 7 and 9
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论