[英]Adding the sum of positive and negative integer in the array c# for loop
The part ""The sum of positive input is:" and "The sum of negative input is:" are not working there's no answer showing what could be the solution? btw this an 11th grade question “正输入的总和是:”和“负输入的总和是:”部分不起作用没有答案显示可能是什么解决方案?顺便说一句,这是一个 11 年级的问题
int i, num, sum = 0;
Console.Write("\nInput the number of elements to be stored in the array :");
num = Convert.ToInt32(Console.ReadLine());
for (i = 0; i < num; i++)
{
Console.Write("Elements - {0}:", i, num);
array[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("The positive inputs are :");
for (i = 0; i < num; i++)
{
if (array[i] >= 0)
{
Console.Write(array[i] + ",");
array[i]++;
}
}
Console.Write("\nThe sum of positive input is :");
for (i = 0; i < num; i++)
{
if (array[i] >= 0)
{
sum += array[i];
}
}
Console.Write("\nThe negative inputs are :");
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
Console.Write(array[i] + ",");
array[i]++;
}
}
Console.Write("\nThe sum of negative input is :");
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
sum += array[i];
}
}
Special thanks to Sir Usama And Sir Drag and Drop特别感谢 Usama 爵士和拖放爵士
int[] array = new int[100];
int i, num, sum = 0;
Console.Write("\nInput the number of elements to be stored in the array :");
num = Convert.ToInt32(Console.ReadLine());
for (i = 0; i < num; i++)
{
Console.Write("Elements - {0}:", i, num);
array[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("The positive inputs are :");
for (i = 0; i < num; i++)
{
if (array[i] >= 0)
{
Console.Write(array[i] + ",");
}
}
Console.Write("\nThe sum of positive input is :" );
for (i = 0; i < num; i++)
{
if (array[i] >= 0)
{
sum += array[i];
}
}
Console.Write(sum);
sum = 0;
Console.Write("\nThe negative inputs are :");
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
Console.Write(array[i] + ",");
}
}
Console.Write("\nThe sum of negative input are :" );
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
sum += array[i];
}
}
Console.Write(sum);
First, you don't initialize your array after you read the array length from user input.首先,从用户输入读取数组长度后,您不会初始化数组。 You should have:
你应该有:
int[] array;
Console.Write("\nInput the number of elements to be stored in the array :");
num = Convert.ToInt32(Console.ReadLine());
array = new int[num];
Second, you are using the same sum
variable for both positive and negative calculation (without resetting it in between).其次,您对正负计算使用相同的
sum
变量(中间不重置)。 You can use two separate variables for these two computations:您可以为这两个计算使用两个单独的变量:
int i, num, posSum = 0, negSum = 0;
Third, as Drag and Drop mentioned in the comment, you don't need that array[i]++;
第三,正如评论中提到的拖放,你不需要那个
array[i]++;
, since it modifies the elements of the array. ,因为它修改了数组的元素。
Fourth, as Usama Aziz mentioned in the comment, you don't have a Console.Write
where you print the results.第四,正如Usama Aziz在评论中提到的那样,您没有
Console.Write
打印结果的位置。
The final solution should look like this:最终解决方案应如下所示:
static void Main(string[] args)
{
int i, num, posSum = 0, negSum = 0;
int[] array;
Console.Write("\nInput the number of elements to be stored in the array :");
num = Convert.ToInt32(Console.ReadLine());
array = new int[num];
for (i = 0; i < num; i++)
{
Console.Write("Elements - {0}:", i, num);
array[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("The positive inputs are :");
for (i = 0; i < num; i++)
{
if (array[i] >= 0)
{
Console.Write(array[i] + ",");
}
}
Console.Write("\nThe sum of positive input is :");
for (i = 0; i < num; i++)
{
if (array[i] >= 0)
{
posSum += array[i];
}
}
Console.Write(posSum);
Console.Write("\nThe negative inputs are :");
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
Console.Write(array[i] + ",");
}
}
Console.Write("\nThe sum of negative input is :");
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
negSum += array[i];
}
}
Console.Write(negSum);
}
Good luck on your exam!祝你考试顺利!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.