C# 如何访问部分初始化数组中不是0的元素?
我想将用户为数组输入的所有值相乘。问题是,如果使用的数组元素少于50个,则结果始终为0。是否有办法仅访问用户输入的元素C# 如何访问部分初始化数组中不是0的元素?,c#,arrays,C#,Arrays,我想将用户为数组输入的所有值相乘。问题是,如果使用的数组元素少于50个,则结果始终为0。是否有办法仅访问用户输入的元素 static void Main(string[] args) { const int SIZE = 50; int[] array = new int[SIZE]; int index = 0; int aScore = 0; do { Conso
static void Main(string[] args)
{
const int SIZE = 50;
int[] array = new int[SIZE];
int index = 0;
int aScore = 0;
do
{
Console.WriteLine("Enter a value for the array for position {0} (or 0 to stop):", (index + 1));
aScore = int.Parse(Console.ReadLine());
if(aScore >= 0)
{
array[index++] = aScore;
}
}while (aScore != 0 && index < SIZE);
Console.WriteLine("The product of the array is: {0}", SumArray(array));
Console.ReadLine();
}
static int SumArray(int[] array)
{
int product = 1;
for (int i = 0; i < array.Length; i++)
{
product *= array[i];
}
return product;
}
static void Main(字符串[]args)
{
常数int SIZE=50;
int[]数组=新的int[SIZE];
int指数=0;
int aScore=0;
做
{
WriteLine(“为位置{0}(或0停止)的数组输入一个值):”,(索引+1));
aScore=int.Parse(Console.ReadLine());
如果(aScore>=0)
{
数组[index++]=aScore;
}
}而(aScore!=0&&index
确保使用System.Linq时有代码>
正如Tim上面所说的,使用列表将是解决这个问题的首选方法。
如果使用列表
,则可以像这样使用LINQ聚合函数
var ints = new List<int> {1, 2, 3, 4, 5};
var product = ints.Aggregate((total, nextNum) => total * nextNum); // product = 120
static int SumArray(int[] array)
{
int product = 1;
for (int i = 0; i < array.Length; i++)
{
if (array[i] != 0)
{
product *= array[i];
}
}
return product;
}
var ints=新列表{1,2,3,4,5};
var乘积=整数聚合((总计,下一个)=>总计*下一个);//产品=120
编辑
这里有一个完整的例子,我会怎么做
private static void Main()
{
const int maxScores = 50;
int index = 0;
int nextScore = 0;
var scores = new List<int>();
do
{
Console.WriteLine("Enter a value for the array for position {0} (or 0 to stop):", ++index);
nextScore = int.Parse(Console.ReadLine());
if (nextScore > 0)
scores.Add(nextScore);
} while (nextScore != 0 && index < maxScores);
Console.WriteLine("The product of the scores is : {0}",
scores.Aggregate((total, next) => total * next));
Console.ReadLine();
}
private static void Main()
{
const int maxScores=50;
int指数=0;
int-nextScore=0;
var分数=新列表();
做
{
WriteLine(“为位置{0}(或0停止)的数组输入一个值):”,++index);
nextScore=int.Parse(Console.ReadLine());
如果(NextCore>0)
分数。添加(nextScore);
}while(nextScore!=0&&index总计*下一个));
Console.ReadLine();
}
相乘前检查0:
if (array[i] != 0)
product *= array[i]
此外,如果要对数组进行“求和”,则操作应为您的方法
static int SumArray(int[] array)
{
int product = 1;
for (int i = 0; i < array.Length; i++)
{
product *= array[i];
}
return product;
}
静态int-SumArray(int[]数组)
{
int乘积=1;
for(int i=0;i
应该是这样的
var ints = new List<int> {1, 2, 3, 4, 5};
var product = ints.Aggregate((total, nextNum) => total * nextNum); // product = 120
static int SumArray(int[] array)
{
int product = 1;
for (int i = 0; i < array.Length; i++)
{
if (array[i] != 0)
{
product *= array[i];
}
}
return product;
}
静态int-SumArray(int[]数组)
{
int乘积=1;
for(int i=0;i
而且
do
{
Console.WriteLine("Enter a value for the array for position {0} (or 0 to stop):", (index + 1));
aScore = int.Parse(Console.ReadLine());
if(aScore >= 0)
{
array[index++] = aScore;
}
}while (aScore != 0 && index < SIZE);
do
{
WriteLine(“为位置{0}(或0停止)的数组输入一个值):”,(索引+1));
aScore=int.Parse(Console.ReadLine());
如果(aScore>=0)
{
数组[index++]=aScore;
}
}而(aScore!=0&&index
这部分不是实现目标的好方法,因为您也将值为0的记录放在数组中。您可以将其替换为
如果(aScore>0)
如果您愿意,但我建议您使用List而不是数组,这样您就不会有额外不必要元素的问题。您可以完全放弃SumArray()
方法,只需说:
int product = array
.Where( n => n != 0 )
.Aggregate( 1 , (m,n) => m*n )
;
Console.WriteLine( "The product of the array is: {0}" ,
) ;
您可以使用列表
(实际上,它只是封面下的一个可调长度数组),而不必担心:
static void Main( string[] args )
{
List<int> scores = new List<int>() ;
while ( true )
{
Console.WriteLine( "Enter a value for the array for position {0} (or 0 to stop):" ,
1+scores.Count
) ;
int score = int.Parse( Console.ReadLine() ) ;
if ( score == 0 ) break ;
values.Add(score);
}
int product = scores.Aggregate( 1 , (m,n) => m*n ) ;
Console.WriteLine( "The product of the array is: {0}" , product ) ;
return ;
}
如果在for循环内测试If(array[i]>0)
,则可以进行多次测试。例如,如果您通知三个数字,则将对该条件进行47次测试
所以,如果你找到一个零,最好打破这个循环
<!-- language: lang-cs -->
static int SumArray(int[] array)
{
int product = 1;
for (int i = 0; i < array.Length; i++)
{
if (array[i] == 0) break;
product *= array[i];
}
return product;
}
静态int-SumArray(int[]数组)
{
int乘积=1;
for(int i=0;i
为什么不使用列表?我看到一些C习惯。在函数开头收集变量并初始化它们。固定大小的缓冲区。对于循环。C#如果你把它们留下,效果会更好。