我會如何將陣列分成幾個區?例如,我想要求用戶輸入說12個值。然後,將它們分成4個季度並在每個季度添加值。我想如何將所有這些添加,但我堅持如何分組添加它們。 謝謝。如何將數組分組?
import java.util.Scanner;
import java.util.stream.*;
public class SalesData {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int[] salesData = new int[12];
int monthNumber=1;
for (int i = 0; i < 12; i++) {
System.out.println("Please enter the data for month "+monthNumber);
salesData[i] = keyboard.nextInt();
int newNumber=monthNumber++;
}
System.out.println("The first quarter total is ");
System.out.println("The second quarter total is ");
System.out.println("The third quarter total is ");
System.out.println("The fourth quarter total is ");
double sum = IntStream.of(salesData).sum();
System.out.println("The Annual Sales Total is "+sum);
}//end main
}`
創建取作爲參數,陣列,起始索引和結束索引的方法,該方法將返回總和 –