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
热门推荐
10 女儿出阁的祝福语简短
11 新年公司祝福语简短牛年
12 幽默祝福语押韵简短
13 新郎朋友祝福语简短大气
14 对女儿生日祝福语简短
15 完成预算祝福语简短精辟
16 年底了祝福语简短的
17 新年祝福语简短俏皮文案
18 白族结婚祝福语简短大全