自定义Java循环的分隔符输出

huangapple go评论109阅读模式
英文:

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 &lt;= bil; i++){
			if (i%2!=0)
			System.out.print(i+&quot; &quot;);
		}

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 &lt;= bil; i++) {
	if (i % 2 != 0) {
		if (buf.length() != 0) {
			lastComma = buf.length();
			buf.append(&quot;, &quot;);
		}
		buf.append(i);
	}
}
if (lastComma != 0)
	buf.replace(lastComma, lastComma + 1, &quot; and&quot;);
System.out.print(buf.toString());

Results with various values of bil

bil=1   -&gt;  1
bil=2   -&gt;  1
bil=3   -&gt;  1 and 3
bil=4   -&gt;  1 and 3
bil=5   -&gt;  1, 3 and 5
bil=6   -&gt;  1, 3 and 5
bil=7   -&gt;  1, 3, 5 and 7
bil=8   -&gt;  1, 3, 5 and 7
bil=9   -&gt;  1, 3, 5, 7 and 9
bil=10  -&gt;  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 = &quot;&quot;;
    for (int i = 1; i &lt;= bil; i++) {
        if (i % 2 != 0)
            System.out.print(separator + i);
        separator = i + 2 &gt;= bil ? &quot; and &quot; : &quot;, &quot;;
    }
    System.out.println();
}

and

for (int i = 1; i &lt; 10; ++i) {
    System.out.print(&quot;bil=&quot; + i + &quot; : &quot;);
    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(&quot;, &quot;, &quot;&quot;, &quot;&quot;);
String tmp = null;
for (int i = 1; i &lt;= bil; i++) {
    if (i % 2 != 0) {
        if (tmp != null) {
            sj.add(tmp);
        }
        tmp = Integer.toString(i);
    }
}
var res = sj.length() &gt; 0
        ? sj.toString() + &quot; and &quot; + 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

huangapple
  • 本文由 发表于 2020年8月28日 11:40:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63627116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定