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
热门推荐
5 短祝福语简短暖心
10 结婚祝福语粤语大全简短
11 晚上祝福语女生文案简短
12 法语妈妈生日祝福语简短
13 药厂开工祝福语大全简短
14 蛋糕节日祝福语简短英文
15 跨年的生日祝福语简短
16 文案祝福语英文短句简短
17 在家聚餐婚礼祝福语简短
18 学生节祝福语大全简短