输出结果的时候 最后一个数字后面的逗号怎么去掉?

public class Test03_2 {
    public static void main(String[] args) {
        //附加需求:统计这些数据的总个数
        int count=0;
        System.out.print("[");
        for(int x=1;x<=100;x++){
            if(x%7==0||x%10==7||x/10%10==7){
                System.out.print(x+",");
                count++;
            }
        }
        System.out.println("]");
        System.out.println("逢七过的总个数是:"+count);
    }
}

 

if(x==100) print x

为什么不直接  新建一个数组  添加进去呢

无法确定末尾的话,可以换一种思路,确定开头,把‘,’放在数字前面,通过判断初始数的方式:

for(int x=1;x<=100;x++){
    if(x%7==0||x%10==7||x/10%10==7){
        count == 0 ? System.out.print(x) : System.out.print(","+x);
        count++;
    }
}