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 婚前女儿生日祝福语简短