不想最后打印逗号 [英] do not want to print comma in the end

查看:66
本文介绍了不想最后打印逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历应该以这种方式显示的数组:

I am iterating through an array which should be displayed in this manner:

    k,j,i,h,g,f,e,d,c,b,a,
a,a,a,a,a,a,a,a,a,a,a,
b,b,b,b,b,a,a,a,a,a,a,
z,y,x,w,v,u,t,s,r,q,p,

,但最后没有逗号。

for ( int k = 0;  k < lineChar.length;  k++ ) //for printing in reverse order
            {
                if(lineChar[n] != ','){

                System.out.print(lineChar[n]);
                if(k == lineChar.length-1)
                   System.out.print("");
                else
                   System.out.print(",");

                --n;
                }
            }
            System.out.println();


推荐答案

int n=lineChar.lenght-1;

//Next 2 lines are edit after comment by @aix
while ((lineChar[n]==',') && (n>=0)) n--;
if (n>=0) System.out.print(lineChar[n--]);

for (;  n >=0;  n-- ) {
  if(lineChar[n] != ',') {
    System.out.print(",");
    System.out.print(lineChar[n]);
  }
}

这篇关于不想最后打印逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆