Java 数组元素乘法程序。

java 8object oriented programmingprogramming更新于 2025/8/20 14:36:17

计算数组元素的乘积。

  • 创建一个空变量。(乘积)
  • 将其初始化为 1。
  • 循环遍历每个元素(或从用户那里获取每个元素),将每个元素乘以乘积。
  • 打印乘积。

示例

import java.util.Arrays;
import java.util.Scanner;
public class ProductOfArrayOfElements {
   public static void main(String args[]){
      System.out.println("Enter the required size of the array :: ");
      Scanner s = new Scanner(System.in);
      int size = s.nextInt();
      int myArray[] = new int [size];
      int product = 1;
      System.out.println("Enter the elements of the array one by one ");
      for(int i=0; i<size; i++){
         myArray[i] = s.nextInt();
         product = product * myArray[i];
      }
      System.out.println("Elements of the array are: "+Arrays.toString(myArray));
      System.out.println("Sum of the elements of the array ::"+product);
   }
}

输出

Enter the required size of the array:
5
Enter the elements of the array one by one
11
65
22
18
47
Elements of the array are: [11, 65, 22, 18, 47]
Sum of the elements of the array:13307580

相关文章