我想用括號和逗號在我的程序中打印數組。這裏是我的代碼:在Java中使用括號和逗號顯示數組輸出?
public static void main(String[] args) {
int[] arrayIntList = new int[10]; // Starting the array with the specified length
int sum = 0; // Defining the sum as 0 for now
// Using the for loop to generate the 10 random numbers from 100 to 200, inclusive.
for(int nums1 = 0; nums1 < arrayIntList.length; nums1++) {
arrayIntList[nums1] = (int)(100 + Math.random()*101);
}
Arrays.sort(arrayIntList); // Sorting the array list
System.out.print("[");
for(int i = 0; i < arrayIntList.length; i++) { // Printing the array
System.out.print(arrayIntList[i] + " ");
}
System.out.print("]");
int[] arrayC = CalcArray(arrayIntList); // Sending the array to the method
System.out.println("");
for(int doubles : arrayC) {
System.out.print(doubles + " "); // Printing the output from the second method and calculating the sum
sum = sum + doubles;
}
System.out.printf("%nThe total is %,d", sum); // Printing the sum
}
private static int[] CalcArray(int[] nums) {
for(int nums2 = 0; nums2 < nums.length; nums2++) { // Doubling the original array
nums[nums2] *= 2;
}
return nums; // Returning the doubles numbers
}
我正在尋找的格式就像[1,2,3,4,5,6]。 如果有人能給我一些指示,那麼這將是很好的。 謝謝!
首先downvote,hooray! – AvenNova
你現在得到什麼?你需要把更多的信息放在這個問題上 – techspider