避免打印最后一个逗号

我正在尝试打印此循环而不使用最后一个逗号。 我一直在谷歌搜索这个,从我看到的一切似乎过于复杂这么小的问题。 当然有一个简单的解决方案,以避免打印最后一个逗号。 非常感谢,如果有人能帮助我,这让我疯了!

例如,它从1-10 // 1,2,3,4,5,6,7,8,9,10循环,<不要这个最后一个逗号

public static void atob(int a,int b) { for(int i = a; i <= + b; i++) { System.out.print(i + ","); } } 

我可能因为这个答案而被石头砸死了

 public static void atob(int a,int b) { if(a<=b) { System.out.println(a); for(int i = a+1;i<=b;i++) { System.out.println(","+i); } } } } 

尝试这个

 public static void atob(int a,int b) { for(int i = a; i < b; i++) { System.out.print(i + ","); } System.out.print(b); } 

另一种方法是这样做。

 String sep = ""; for(int i = a; i <= b; i++) { System.out.print(sep + i); sep = ","; } 

如果您使用的是StringBuilder

 StringBuilder sb = new StringBuilder(); for(int i = a; i <= b; i++) sb.append(i).append(','); System.out.println(sb.subString(0, sb.length()-1)); 

Java 8:

 IntStream.rangeClosed(a, b) .collect(Collectors.joining(",")); 

如果要执行交错操作:

Java 8:

 IntStream.rangeClosed(a, b) .peek(System.out::print) .limit(b - a) // [a:(b-1)] .forEach(i -> System.out.print(",")); 

Java 9:

 IntStream.rangeClosed(a, b) .peek(System.out::print) .takeWhile(i -> i < b) // [a:(b-1)] .forEach(i -> System.out.print(",")); 
 public static void atob(int a, int b) { if (a < b) { System.out.print(a); while (a < b) { a++; System.out.print("," + a); } } } 

当被召唤时

 atob(0,10); 

会给出输出

0,1,2,3,4,5,6,7,8,9,10

一般方法可以是区分第一项和所有其他项。 第一次运行,在我之前没有打印逗号。 之后,每次都会在我之前打印逗号。

 public static void atob(int a,int b) { boolean first = true; for(int i = a; i <= + b; i++) { if ( first == false ) System.out.print(","); System.out.print(i); first = false; } } 

在这种特定情况下,使用三元运算符(http://en.wikipedia.org/wiki/Ternary_operation),您可以将其写为紧凑的:

 public static void atob(int a,int b) { for(int i = a; i <= + b; i++) { System.out.print( i + ( i < b ? "," : "" ) ); } } 

如果没有三元操作,这将看起来像这样(并且更具可读性,这通常是一件好事):

 public static void atob(int a,int b) { for(int i = a; i <= + b; i++) { System.out.print( i ); if ( i < b ) System.out.print( "," ); } } 

(注意+ b与b相同,所以你可以替换它,正如其他人在答案中所做的那样)

稍作调整(逗号后的方法名称,变量和空格):

 public static void printSequence(int start, int end) { if (end < start) { return; // or however you want to handle this case } if (end == start) { System.out.print(start); return; } StringBuilder sequence = new StringBuilder(); for (int i = start; i <= end; i++) { sequence.append(i).append(", "); } // now simply print the sequence without the last ", " System.out.print(sequence.substring(0, sequence.length() - 2)); } 

尝试:

 public static void atob(int a, int b) { if (b < a) { final int temp = a; a = b; b = temp; } System.out.print(a++); for (int i = a; i < b ; i ++ ) { System.out.print("," + i); } } 

使用StringBuilder。 使用基于子串的索引。

 public class arraySort { public static void main(String[] args) { int a[] = { 2, 7, 5, 6, 9, 8, 4, 3, 1 }; for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { if (a[i] < a[j]) { int temp; temp = a[i]; a[i] = a[j]; a[j] = temp; } } } System.out.print("Descending order:{"); StringBuilder br = new StringBuilder(); for (int i = 0; i < a.length; i++) { br.append(a[i] + ","); } System.out.print( br.substring(0, br.length()-1)); System.out.println("}"); } } 
 public static void atob (int a,int b){ for(int i = a; i <= b; i++) { System.out.print(i ); if(i 

笨重的解决方案,但嘿 – 它的工作原理。

 public static void atob(int a, int b){ int count = 1; for(int i = a; i <= + b; i++) { System.out.print(i); if(count!=((b + 1) - a)) { System.out.print(", "); count++; } } } 

或者,这也有效。

 public static void atob(int a, int b){ int count = 0; for(int i = a; i <= + b; i++) { if(count > 0){ System.out.print(", "); } System.out.print(i); count++; } } 

找到另一种使用正则表达式的简单方法:

 String str = "abc, xyz, 123, "; str = str.replaceAll(", $", ""); System.out.println(str); // "abc, xyz, 123" 

您可以简单地使用退格序列作为退格(\ b),在主循环之外,它将删除最后一个逗号。

 public static void atob(int a,int b) { for(int i = a; i <= + b; i++) { System.out.print(i + ","); } System.out.print("\b"); }