if (authors.length >= 1) {
System.out.print(authors[0]);
}
for (int i = 1; i < authors.length; i++) {
System.out.print("; " + authors[i]);
}
Run Code Online (Sandbox Code Playgroud)
所以这个的输出是author1; author2; author3
如何将其改为author1; author2 & author3?如果只有2位作者,那么输出应该是author1 & author2.提前致谢.
您只需要在循环中添加一个条件来处理最后一种情况:
for (int i = 1; i < authors.length; i++) {
if(i == authors.length - 1)
System.out.print("& " + authors[i]);
else
System.out.print("; " + authors[i]);
}
Run Code Online (Sandbox Code Playgroud)