我正努力在編程Java方面做得更好,我是一名新手,並且在我明年冒險上大學之前,在我的空閒時間裏學會了如何編寫基本程序和基本數據結構!更好的方法來反覆寫一個for循環?
那麼有沒有更好的方法來寫這段代碼?
int p;
for (p = 0; p < 10; p++) {
System.out.print(time[p] + ",");
}
System.out.println();
System.out.println();
System.out.println();
for (p = 0; p < 10; p++) {
System.out.print(something[p] + ",");
}
System.out.println();
System.out.println();
System.out.println("somethingagain1[p]");
for (p = 0; p < 10; p++){
System.out.print(something again2[p] + ",");
}
您可以創建一個參數作爲消息來顯示,長度等功能,並在您想用循環打印時調用該函數 –
而不是編寫3'println()',您可以直接寫'System.out。 println(「\ n \ n」);'這樣'\ n'表示換行。 – Nerzid
你可以使用'Arrays'庫中的'Arrays.toString(array)'來打印一個數組,而不需要使用for循環。 – EvanL00