使用stream.collect(Collectors.joining(", "))我可以轻松地加入由逗号分隔的流的所有字符串.可能的结果是"a, b, c".但是,如果我希望最后一个分隔符不同,该怎么办呢?例如是" and "这样的,我得到"a, b and c"的结果.有一个简单的解决方案吗?
像往常一样,我只是另一个绝望的高中生,在经过大量的谷歌搜索后,他们不得不诉诸问题.
我正在研究其中一个可以在现实生活中应用的程序(但从来没有).在这种情况下,一个更改计数器,总计一角硬币,镍币,四分之一和剩余的分数,并用一句话显示它们.
我的问题是,我需要制作一个正确的句子格式,最后使用逗号和"和",这是我用我非常有限的当前知识来管理的.
我的计划部分涉及我想问的是:
System.out.print(backupInput + ": ");
// Outputting the quarters
if (quarters > 0)
{
if (quarters == 1)
System.out.print("one quarter, ");
else
System.out.print(quarters + " quarters, ");
}
// Outputting the dimes
if (dimes > 0)
{
if (dimes == 1)
System.out.print("one dime, ");
else
System.out.print(dimes + " dimes, ");
}
// Outputting the nickels
if (nickels > 0)
{
if (nickels == 1)
System.out.print("one nickel, ");
else
System.out.print(nickels + " nickels ");
}
// …Run Code Online (Sandbox Code Playgroud)