C# 程序可将列表中的所有数字相乘
首先,设置列表 −
List<int> myList = new List<int> () {
5,
10,
7
};现在,将变量的值设置为 1,它将帮助我们进行乘法 −
int prod = 1;
循环并获取产品 −
foreach(int i in myList) {
prod = prod*i;
}以下为代码 −
示例
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List<int> myList = new List<int>() {
5,
10,
7
};
Console.WriteLine("List: ");
foreach(int i in myList) {
Console.WriteLine(i);
}
int prod = 1;
foreach(int i in myList) {
prod = prod*i;
}
Console.WriteLine("Product: {0}",prod);
}
}输出
List: 5 10 7 Product: 350
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP