我將字符串轉換爲數組並將其轉換爲整數數組。我也將數組中的所有值乘以3,並且它可以工作。我的問題是我如何製作,以便輸出結果只顯示[3,6,9,15],而不是下面顯示的內容。將Java字符串轉換爲數組
public static void main(String[] args) {
String arr = "1,2,3,5";
String[] items = arr.split(",");
int[] results = new int[items.length];
for (int i = 0; i < items.length; i++) {
results[i] = Integer.parseInt(items[i]);
results[i] *= 3;
System.out.println(java.util.Arrays.toString(results));
}
}
OUTPUT:
[3, 0, 0, 0]
[3, 6, 0, 0]
[3, 6, 9, 0]
[3, 6, 9, 15]
移動的'的System.out.println(...)'外循環的位置? – KevinO