如何在 Java 中使用 for 循环在数组中的每个数字后添加通讯

How to add comms after every number from array using for loop in Java

我正在尝试输出:65, 3, 10(末尾没有逗号)

但是使用我的代码,我得到了这个:65, 3, , 10

谁能帮我写出正确的代码? 这是我的代码:

static void printArray(int[] validInput, int arrayFill){    //validInput[] = {65, 3, 10};  arrayFill = 3;(size of array)
    for(int i = 0; i < arrayFill; i++)
    {
        for(int j = 0; j < i; j++)
        {
            System.out.print(", ");
        }
        System.out.print(validInput[i]);
    }
}

我不知道你为什么使用 double for 但是使用这个解决方案(当索引等于最后一个循环时停止打印 ',' 的 if 条件的 for)你有所需的输出

static void printArray(int[] validInput, int arrayFill){    //validInput[] = {65, 3, 10};  arrayFill = 3;(size of array)
    for(int i = 0; i < arrayFill; i++)
    {
        System.out.print(validInput[i]);
        
        if(i!=arrayFill-1) {
             System.out.print(", ");
        }
    }
}

结果:

65, 3, 10